Java IO系列(四):文件压缩GZIP、Zip

来源:互联网 发布:淘宝拍照灯光怎么打 编辑:程序博客网 时间:2024/06/05 07:56

转载请注明出处:http://blog.csdn.net/jeffleo/article/details/52266360

有必要多看几遍的

这里写图片描述
关于字符和字节,例如文本文件,XML这些都是用字符流来读取和写入。而如RAR,EXE文件,图片等非文本,则用字节流来读取和写入。

压缩类

有关压缩的类如下:
DeflaterOutputStream:压缩类的基类
ZipOutputStream:将数据压缩成Zip格式
GZIPOutputStream:将数据压缩成GZIP格式
CheckedOutputStream:为OutputStream生成校检和

InflaterInputStream:解压缩类的基类
ZipInputStream:解压Zip格式数据
GZIPInputStream:解压GZIP格式数据
CheckedInputStream:为InputStream生成校检和

对单个文件压缩——GZIPOutputStream和GZIPInputStream

当我们需要对单个数据流进行压缩,使用GZIP比较合适。

public class TestZipIn {    public static void main(String[] args) throws IOException {        /*         * 压缩数据         */        BufferedReader reader  = new BufferedReader(new FileReader("E:\\test\\hello.txt"));        BufferedOutputStream out = new BufferedOutputStream(                new GZIPOutputStream(new FileOutputStream("E:\\test\\test.gz")));        int c;        while((c = reader.read()) != -1){            out.write(c);        }        reader.close();        out.close();        /*         * 读取压缩数据         */        BufferedReader reader2  = new BufferedReader(                new InputStreamReader(new GZIPInputStream(new FileInputStream("E:\\test\\test.gz"))));        String s;        while((s = reader2.readLine()) != null){            System.out.println(s);        }    }}

上面实现了了把hello.txt文件的数据复制到了压缩文件test.gz中,并对他所文件进行读取。

对单个文件压缩——ZipOutputStream和ZipInputStream

压缩

Zip格式文件可以与下载的压缩工具更好的使用,所以多数情况下还是使用Zip。

public class TestZIP {    public static void main(String[] args) throws IOException {        FileOutputStream fos = new FileOutputStream("E:\\test\\hello.zip");        CheckedOutputStream cos = new CheckedOutputStream(fos, new Adler32());        ZipOutputStream zos = new ZipOutputStream(cos);        BufferedOutputStream out = new BufferedOutputStream(zos);        zos.setComment("a test of zip");        String[] files = {"hello.txt", "new.txt", "old.txt"};        String path = "E:\\test\\";        for (String file : files) {            System.out.println("write file " + file);            BufferedReader in = new BufferedReader(new FileReader(path + file));            zos.putNextEntry(new ZipEntry(file));            int c;            while((c = in.read()) != -1){                out.write(c);            }            in.close();            out.flush();        }        out.close();    }}

上面实现了三个文件hello.txt”, “new.txt”, “old.txt”压缩成一个hello.zip的文件。ZipEntry表示Zip文件中的一个入口,上面三个文件,在Zip中则对应三个入口。每写入一个文件,就要为该文件打开一个入口。
CheckedOutputStream cos = new CheckedOutputStream(fos, new Adler32())生成了校检码,其中有两种方式:Adler32(快一些)和CRC32(慢一些,但更安全)。

解压缩

public class TestZIP {    public static void main(String[] args) throws IOException {        FileInputStream fis = new FileInputStream("E:\\test\\hello.zip");        CheckedInputStream cis = new CheckedInputStream(fis, new Adler32());        ZipInputStream zis = new ZipInputStream(cis);        BufferedReader br = new BufferedReader(new InputStreamReader(zis));        ZipEntry ze;        while((ze = zis.getNextEntry()) != null){//循环读文件            System.out.println("reading " + ze);            String x;            while((x = br.readLine()) != null){                System.out.println(x);            }        }        System.out.println(cis.getChecksum().getValue());        br.close();    }}

其中,cis.getChecksum().getValue()返回校检码。解压缩的方法和顺序,和压缩的方法顺序相反。

0 0