Java流编程实例之七--压缩流

8. 压缩流

压缩流可以将输入的数据变为压缩格式后进行输出,或者读取压缩格式的数据后,解压为正常数据。

8.1 将一个文件压缩为一个压缩文件

注意ZipEntry的使用,一个ZipEntry代表压缩文件中的一个文件入口。

import java.io.*;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

/**
 * Created by test2 on 2016/8/19.
 */
public class ZipStreamExam1 {
    public static void main(String[] args) {
        try {
            BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream("D:\\input.txt"));
            ZipOutputStream zipOutputStream = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream("d:\\output.zip")));
            byte[] buf = new byte[1024];
            int len = 0;
            ZipEntry ze = new ZipEntry("input.txt");
            zipOutputStream.putNextEntry(ze);
            while ((len = bufferedInputStream.read(buf)) != -1) {
                zipOutputStream.write(buf, 0, len);
            }
            zipOutputStream.flush();
            zipOutputStream.close();
            bufferedInputStream.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

8.2 将一个多重目录压缩为一个文件

代码中定义使用了递归函数zipDir:

import java.io.*;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class ZipStreamExam2 {
    public static void main(String[] args) {
        try {
            File file = new File("d:\\zipmultidir");
            ZipOutputStream zos = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream("d:\\zipmultidir.zip")));
            zipDir(file, zos, file);
            zos.flush();
            zos.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //压缩一个目录至zip文件
    private static void zipDir(File dir, ZipOutputStream zos, File rootDir) throws IOException {
        if (!dir.isDirectory())
            return;

        File[] files = dir.listFiles();
        for (int i = 0; i < files.length; i++) {
            if (files[i].isFile()) {
                System.out.println(files[i].getAbsolutePath());
                String now = files[i].getAbsolutePath();
                String root = rootDir.getAbsolutePath();
                String name = now.substring(root.length() + 1);
                System.out.println(name);

                FileInputStream fis = new FileInputStream(files[i]);

                byte buf[] = new byte[1024];
                int len = 0;
                ZipEntry ze = new ZipEntry(name);
                zos.putNextEntry(ze);
                while ((len = fis.read(buf)) != -1) {
                    zos.write(buf, 0, len);
                }
                fis.close();
            } else if (files[i].isDirectory()) {
                zipDir(files[i], zos, rootDir);
            }
        }
    }
}

8.3 将一个压缩文件解压到当前目录

该压缩文件中可能有多重目录结构。

import java.io.*;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

/**
 * Created by test2 on 2016/8/19.
 */
public class ZipStreamExam3 {
    public static void main(String[] args) {
        try {
            File srcFile = new File("d:\\zipmultidir.zip");
            System.out.println(srcFile.getCanonicalPath());
            String curDir = srcFile.getParent()+File.separator+"destDir"+File.separator;

            ZipInputStream zipInputStream = new ZipInputStream(new BufferedInputStream(new FileInputStream(srcFile)));
            ZipEntry ze = null;
            byte[] buf = new byte[1024];
            int len = 0;
            while ((ze = zipInputStream.getNextEntry()) != null) {
                String filePath = curDir + ze.getName();
                File destFile = new File(filePath);
                File destDir = new File(destFile.getParent());
                if(!destDir.exists()){
                    destDir.mkdirs();
                }
                BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(destFile));
                while ((len = zipInputStream.read(buf)) != -1) {
                    bufferedOutputStream.write(buf, 0, len);
                }
                bufferedOutputStream.flush();
                bufferedOutputStream.close();
            }
            zipInputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值