java 压缩技术

时间:2022-03-31 01:44:30

package zip;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.zip.Deflater;
import java.util.zip.DeflaterOutputStream;
import java.util.zip.Inflater;
import java.util.zip.InflaterInputStream;

public class Test {


public static void main(String[] args) {

String inStr = "@UEsDBBQACAgIAFZ2dUkAAAAAAAAAAAAAAAABAAAAMO1dXXPTxhr+K7npcJVasnEaGHU7fBwK05IO"
+ "MzAQbjKJbDl2/IXtJISrlIGSHBKSc06mFEhpoQPlAMmcM6UQIIQfU8t2rvgLXWkl7btKlNiJnEjx"
+ "OwPW7vvsh1abXT3P+64T5auruWzXWLJUThfyXx6SP5cOdSXzaiGRzqe+PHTh/Knu3kNfEaVElGSa"
+ "KBHjI5kzEjnDkiD50WzWMCeIog0SSYnQT0UdJtGoEqEXpVQh311UIvSiqCo50ddPzSpNl6yaNKFU"
+ "EiQqyT3dskz/dUnSUYm2Q41K0mmfZtSCXaVAq6hWhiaUcp7ISoR+KsWKZaYJJZUmZ5QI/VSKJbu4"
+ "kVIy2kBlopg07tZOKsXywGDOLmSmDZM6yk00rZQ1Wj1ifmZT5NqwEqEXpWz+H6iQS2f6T3zzNS1g"
+ "ZAxTmZiZspkBo+w1zQnTXLS6MJOGQWOfeXL2QjTaI5sIzZnGCmtQY81rZXLqWP/xc5eYkXWj5Yh8"
+ "5GhMYrYcs5VoY8xQYoYJ3itNm6YMMGWYSQMmdlvOQ2Jp1tY4bGzcqgqNmmVUrYtzrZBTrISRtGya"
+ "kxgkx06et3HjB4umhlStSGK9R9j4zJxdfIjbWdYGCuNaQsRMiw2rIubcnLsSr5EEYzOzNqCJAB9K"
+ "ig9USznWYXLxH8dlSTrsYMMOlibnIrWFt7XFX9YnJ+tPntUWXjdevXUKpp2CGdKYel6fW2r8fkNf"
+ "vl9/+r7+4l7t7uv1u6+i+v/maEqfnv20OqPf/L6xvKLfnKWt1B68ok2zarW7T2gftADrhhagPVld"
+ "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rBfCOyO/p7jCwgwX26F"
+ "nJe5+xnvrS+tdW3KesWYwAzLMke/k2VM94A7mOEcdepvOnTxcJeDSWQSLlAkE/DNJ5IJVzWRT7hA"
+ "kVLANtvvJXNVE6mFCxTZhQsUCYYLFDmGC2yWpyPNQJph5YNPM9rsOQvGIQMkEEgg7HXcovOuRf7g"
+ "5b9rkT60I0yN7AEYkD2wzRMjbsgbwsobMOCmYsBts3c2BtyEl3SQ4wUYcAv4BGHALdjzgwG35rgs"
+ "esLMJDLa/Qq4tTXedsmG8A9xccorLhxAdMVls0Oqin+IC/8QF+7fAdq/2+yR8PUPcbGEdTUvxijN"
+ "kVbIifMX+r752sTYmi2zgmxZJ0hUknu6Zbk7yp4S21bBVmJtI+wzT85eiEZ7rAdq7UkVq2drSyiT"
+ "yyxvbRw52sXRmDVt1uZWou0wg7XtwDVuLXC4s1rbKtzQ2R0NArfpoNX4xDhszNpgNGh0dp2N777L"
+ "VFu/V/ro8YuqwQ0DxluKAvb64VKM+eFTbMsBIqCO/fCLxGBz+OU6kOPIzvyL7YjVxoM/W+F0Nnbo"
+ "ZAXA8xigKHoP2AxF8RqLeYbRgcsElSsq1wB7Ht0J7zN4/gorAAnCCtjbJ6w8PYuoqVBTdbKmAqsv"
+ "TJqqHacqUVLZaZRUQZwtlFQhmiyUVG2TVHgyGdVU2E4me0gpucdTSkHIfTIZQMLJZGDHk8neJ5PB"
+ "Y8KTyao/bNffNzJsDU8mH/gJCiez7Zz5CQCZDdDJZG8yiyeTzSQy2gNxMpklXCfb+GFcg9Zpqi3w";
System.err.println("字节压缩/解压缩测试");
System.err.println("输入字符串:\t" + inStr);
byte[] input = inStr.getBytes();
System.err.println("输入字节长度:\t" + input.length);

byte[] data = compress(input);
System.err.println("压缩后字节长度:\t" + data.length);

byte[] output = decompress(data);
System.err.println("解压缩后字节长度:\t" + output.length);
String outputStr = new String(output);
System.err.println("输出字符串:\t" + outputStr);


}

/**
* 压缩
*
* @param data
* 待压缩数据
* @return byte[] 压缩后的数据
*/
public static byte[] compress(byte[] data) {
byte[] output = new byte[0];

Deflater compresser = new Deflater();

compresser.reset();
compresser.setInput(data);
compresser.finish();
ByteArrayOutputStream bos = new ByteArrayOutputStream(data.length);
try {
byte[] buf = new byte[1024];
while (!compresser.finished()) {
int i = compresser.deflate(buf);
bos.write(buf, 0, i);
}
output = bos.toByteArray();
} catch (Exception e) {
output = data;
e.printStackTrace();
} finally {
try {
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
compresser.end();
return output;
}

/**
* 压缩
*
* @param data
* 待压缩数据
*
* @param os
* 输出流
*/
public static void compress(byte[] data, OutputStream os) {
DeflaterOutputStream dos = new DeflaterOutputStream(os);

try {
dos.write(data, 0, data.length);

dos.finish();

dos.flush();
} catch (IOException e) {
e.printStackTrace();
}
}

/**
* 解压缩
*
* @param data
* 待压缩的数据
* @return byte[] 解压缩后的数据
*/
public static byte[] decompress(byte[] data) {
byte[] output = new byte[0];

Inflater decompresser = new Inflater();
decompresser.reset();
decompresser.setInput(data);

ByteArrayOutputStream o = new ByteArrayOutputStream(data.length);
try {
byte[] buf = new byte[1024];
while (!decompresser.finished()) {
int i = decompresser.inflate(buf);
o.write(buf, 0, i);
}
output = o.toByteArray();
} catch (Exception e) {
output = data;
e.printStackTrace();
} finally {
try {
o.close();
} catch (IOException e) {
e.printStackTrace();
}
}

decompresser.end();
return output;
}

/**
* 解压缩
*
* @param is
* 输入流
* @return byte[] 解压缩后的数据
*/
public static byte[] decompress(InputStream is) {
InflaterInputStream iis = new InflaterInputStream(is);
ByteArrayOutputStream o = new ByteArrayOutputStream(1024);
try {
int i = 1024;
byte[] buf = new byte[i];

while ((i = iis.read(buf, 0, i)) > 0) {
o.write(buf, 0, i);
}

} catch (IOException e) {
e.printStackTrace();
}
return o.toByteArray();
}
}

测试结果:

java 压缩技术