The Java I / O support reading and writing compressed data stream format, we can be encapsulated in order to achieve the purpose of compression and decompression.
These classes are not from the Reader, Writer inherited, but belongs InputStream, OutputStream part of the inheritance system. That they are facing operations on bytes. If we have to operate character-oriented, you can use InputStreamReader, OutputStreamWriter be easily converted.
Compression class | Features |
---|---|
CheckedInputStream | GetCheckSum () and a parity any inputStream |
CheckOutputStream | GetCheckSum () and a parity any Outputstream |
DeflaterOutputStream | Compressing the base class |
ZipOutputStream | DeflaterOutputStream implementation class, the data stream may be compressed as a Zip file format |
GZIPOutputStream | DeflaterOutputStream implementation class, the data stream may be compressed to GZIP file format |
InflaterInputStream | Decompression base class class |
ZipInputStream | InfalterInputStream implementation class, you can decompress Zip file format |
GZIPInputStream | InfalterInputStream implementation class, you can decompress Zip file format |
GZIP compression and de-compression format
GZIP is relatively simple, suitable for compression and decompression of a single file compression.
package com.mfs.io;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;
public class GZIPCompress {
/*
* 压缩
* 参数: file指定要压缩的文件,path指定压缩后的文件的存储路径
* 返回值:返回压缩后的文件的绝对路径
*/
public String compress(String file,String path) throws IOException {
BufferedInputStream in = new BufferedInputStream(new FileInputStream(new File(file)));
BufferedOutputStream out = new BufferedOutputStream(new GZIPOutputStream(new FileOutputStream(new File(path + "/GZIPCompress.gz"))));
byte[] b = new byte[1024];
int n = 0;
while ((n = in.read(b)) != -1) {
out.write(b, 0, n);
}
in.close();
out.close();
return new File(path + "/GZIPcompress.gz").getAbsolutePath();
}
/*
* 解压缩
* 参数: 要解压缩的目标文件
*/
public void deCompress (String file) throws FileNotFoundException, IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(new GZIPInputStream(new FileInputStream(new File(file))))); //使用InputStream把面向字节流的对象转换成面向字符流的对象
String s;
while ((s = in.readLine()) != null) {
System.out.println(s);
}
in.close();
}
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
GZIPCompress gz = new GZIPCompress();
String GZIPFile = gz.compress("src/com/mfs/io/GZIPCompress.java", "resource"); //将当前文件压缩压缩到本项目的resource文件夹下
gz.deCompress(GZIPFile); //解压缩压缩后的文件
}
}
Zip file compression and decompression of compressed
Zip has a separate class, contains very comprehensive, it can support multiple compressed files, compressed files follow standard zip standards, so it's easy to collaborate with compression software market .
package com.mfs.io;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.zip.Adler32;
import java.util.zip.CheckedInputStream;
import java.util.zip.CheckedOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;
public class ZipCompress {
/*
* 压缩
* 参数: file指定要压缩的文件,path指定压缩后的文件的存储路径
* 返回值:返回压缩后的文件的绝对路径
*/
public String compress (String[] file,String path) throws IOException {
ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(new File(path + "/ZipCompress.zip")));
CheckedOutputStream csum = new CheckedOutputStream(zos, new Adler32()); //用于产生校验和,可以为任何InputStream产生校验和,有两种方法Adler32(快)、CRC32(慢,准确)
BufferedOutputStream out = new BufferedOutputStream(csum);
for (String f : file) {
BufferedInputStream in = new BufferedInputStream(new FileInputStream(new File(f)));
zos.putNextEntry(new ZipEntry(f)); //每次向压缩文件夹中加入新的文件必掉用putNextEntry()方法,参数为ZipEntry对象
int n = 0;
while ((n = in.read()) != -1) {
out.write(n);
}
in.close();
}
out.close();
//System.out.println(csum.getChecksum().getValue()); //校验和
return new File(path + "/ZipCompress.zip").getAbsolutePath();
}
/*
* 解压缩
* 参数: 要解压缩的目标文件
*/
public void deCompress (String file) throws IOException {
ZipInputStream zis = new ZipInputStream(new FileInputStream(new File(file)));
CheckedInputStream csum = new CheckedInputStream(zis, new Adler32()); //校验和
BufferedReader in = new BufferedReader(new InputStreamReader(csum));
ZipEntry ze;
while ((ze = zis.getNextEntry()) != null) {
String s;
while ((s = in.readLine()) != null) {
System.out.println(s);
}
}
//System.out.println(csum.getChecksum().getValue());
in.close();
}
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
String[] file = new String[] {"src/com/mfs/io/ZipCompress.java","src/com/mfs/io/GZIPCompress.java"};
ZipCompress zip = new ZipCompress();
String zipFile = zip.compress(file, "resource");
zip.deCompress(zipFile);
}
}
For each file to be added to an archive, you must call putNextEntry (), and passing a ZipEntry object. ZipEntry object contains a very comprehensive interface, good enough for the object, we can easily set up and use the compressed file corresponding to the file information, such as: name, compressed and uncompressed file size, date, crc verification and (ZipEntry not supported Adler32).
Unzip the file there is a more simple method is to use ZipFile categories:
public void deCompress2 (String file) throws ZipException, IOException {
ZipFile zip = new ZipFile(new File(file));
Enumeration<? extends ZipEntry> entries = zip.entries();
while (entries.hasMoreElements()) {
ZipEntry ze = entries.nextElement();
BufferedReader in = new BufferedReader(new InputStreamReader(zip.getInputStream(ze)));
String s;
while ((s = in.readLine()) != null) {
System.out.println(s);
}
in.close();
}
}
setComment Zip stream () method can add some comments, but is confusing but there is no method to get the comments, if we want to, then get a comment or put comments inside ZipEntry better.