JAVA 文件与IO 学习总结(五)------zip压缩与解压缩

一.概念

ZipInputStream(InputStream in):创建一个ZipInputStream,使得我们可以从给定的InputStream向其中填充数据

ZipEntry getNextEntry():为下一项返回ZipEntry对象,或者在没有更多项时返回null

ZipOutputStream(OutputStream out):创建一个将压缩数据写出到指定OutputStreamde ZipOutputStream

void putNextEntry(ZipEntry ze):将给定的ZipEntry的信息写出到流中,并定位用于写出数据的流,然后这些数据可以通过write()写出到这个流中。

二.使用

package IO;

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

/**
 * Java实现Zip的压缩与解压缩
 */
public class ZipStreamDemo {
    /**
     * 压缩
     *
     * @param zipFileName 压缩生成的压缩文件名
     * @param targetFile  目标文件
     */
    private static void compression(String zipFileName, File targetFile) {
        System.out.println("开始压缩....");
        try {
            //根据要生成的压缩文件创建相应的压缩流
            ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipFileName));
            //套个缓冲流加强效率
            BufferedOutputStream bos = new BufferedOutputStream(out);
            //开始压缩
            zip(out, targetFile, targetFile.getName(), bos);
            bos.close();
            out.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println("压缩完成");
    }

    /**
     * 压缩方法
     *
     * @param zOut       压缩文件流
     * @param targetFile 目标文件
     * @param name       目标文件的名称
     * @param bos        缓冲流
     */
    private static void zip(ZipOutputStream zOut, File targetFile, String name, BufferedOutputStream bos) throws IOException {
        //如果是目录
        if (targetFile.isDirectory()) {
            File[] files = targetFile.listFiles();
            if (files.length == 0) {//空文件夹
                zOut.putNextEntry(new ZipEntry(name + "/"));//处理空目录
            }
            //其他文件
            for (File f : files) {
                zip(zOut, f, name + "/" + f.getName(), bos);
            }
        } else {  //不是目录是文件
            //新增条目
            zOut.putNextEntry(new ZipEntry(name));
            //创建目标文件的输入流,从中读取数据
            InputStream in = new FileInputStream(targetFile);
            BufferedInputStream bis = new BufferedInputStream(in);
            byte[] bytes = new byte[1024];
            int len = -1;
            while ((len = bis.read(bytes)) != -1) {
                //用嵌套了zip输出流的缓冲流写
                bos.write(bytes, 0, len);
            }
            bis.close();
        }
    }

     /**
     * 解压
     * @param targetFileName  目标文件名称
     * @param parent      解压文件到那个目录下
     */
    private static void decompression(String targetFileName, String parent) {
        try {
            //构建解压的输入流
            ZipInputStream zIn = new ZipInputStream(new FileInputStream(targetFileName));
            ZipEntry entry = null;
            File file = null;
            while ((entry = zIn.getNextEntry()) != null && !entry.isDirectory()) {
                file = new File(parent, entry.getName());
                if (!file.exists()) {
                    new File(file.getParent()).mkdir();//创建次文件的上级目录
                }
                OutputStream out = new FileOutputStream(file);
                BufferedOutputStream bos = new BufferedOutputStream(out);
                byte[] bytes = new byte[1024];
                int len = -1;
                while ((len = zIn.read(bytes)) != -1) {
                    bos.write(bytes, 0, len);
                }
                bos.close();
                System.out.println(file.getAbsolutePath() + "解压成功");
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        //compression("C:\\Users\\sisheng\\Desktop\\test.zip", new File("C:\\Users\\sisheng\\Desktop\\ziptest"));
        decompression("C:\\Users\\sisheng\\Desktop\\test.zip", "C:\\Users\\sisheng\\Desktop\\Zip");
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值