java 处理zip压缩/解压 文件/目录

环境

操作系统: win7
java: jdk7

需求

需要把多个jar进行打包,方便上传。

步骤

这里我们需要用到的java api有:ZipOutputStreamZipEntry

单个文件的压缩

    public static void main(String[] args) {
        //C:\\Users\\yutao\\Desktop\\pageage
        String zipFileName = "C:\\Users\\yutao\\Desktop\\pageage\\ziptest.zip";

        String entry = "C:\\Users\\yutao\\Desktop\\pageage\\ggjob-searchfull.jar";

        compress(zipFileName, entry);
    }

    /**
     * 压缩文件
     * @param zipFileName
     * @param entry
     * @author yutao
     * @date 2017年5月24日下午2:21:11
     */
    private static void compress(String zipFileName, String entry) {

        try {
            ZipOutputStream zos = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(zipFileName)));
            //设置压缩级别
            zos.setLevel(Deflater.BEST_COMPRESSION);

            File entryFile = new File(entry);
//          entryFile.listFiles();

            if(!entryFile.exists()){
                System.out.println("The entry file " + entryFile.getAbsolutePath());
                zos.close();
                return;
            }

            ZipEntry ze = new ZipEntry(entryFile.getName());
            System.out.println(ze.getName());//条码名称
            System.out.println(ze.getComment());
            System.out.println(ze.getCompressedSize());
            System.out.println(ze.getSize());
            System.out.println(ze.getMethod());
            System.out.println(ze.getTime());
            System.out.println(ze.getCrc());
            System.out.println(ze.getExtra());
            zos.putNextEntry(ze);

            BufferedInputStream bis = new BufferedInputStream(new FileInputStream(entryFile));

            byte[] buffer = new byte[1024];
            int count = -1;
            bis.read();

            while((count = bis.read(buffer)) != -1){
                zos.write(buffer, 0, count);
            }
            bis.close();
            zos.closeEntry();
            zos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

多文件压缩

这里写图片描述

下面我们要对上面的情况进行压缩,代码如下:

    public static void main(String[] args) throws Exception {

        String entry = "C:\\Users\\yutao\\Desktop\\pageage\\test";//需要压缩的文件目录
        File file = new File(entry);

        ZipOutputStream zipOutput = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(file.getAbsolutePath() + ".zip")));

        String base = file.getName();

        compressZip(zipOutput, file, base);
        zipOutput.closeEntry();
        zipOutput.close();
    }

    /**
    * 因为子文件夹中可能还有文件夹,所以进行递归
    *
    */
    private static void compressZip(ZipOutputStream zipOutput, File file, String base) throws IOException {

        if(file.isDirectory()){
            File[] listFiles = file.listFiles();// 列出所有的文件

            for(File fi : listFiles){
                if(fi.isDirectory()){
                    compressZip(zipOutput, fi, base + "/" + fi.getName());
                }else{
                    zip(zipOutput, fi, base);
                }
            }
        }else{
            zip(zipOutput, file, base);
        }
    }

    /**
    * 压缩的具体操作
    *
    */
    public static void zip(ZipOutputStream zipOutput, File file, String base) throws IOException, FileNotFoundException {
        ZipEntry zEntry = new ZipEntry(base + File.separator + file.getName());
        zipOutput.putNextEntry(zEntry);

        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));

        byte[] buffer = new byte[1024];
        int read = 0;
        while((read =bis.read(buffer)) != -1){
            zipOutput.write(buffer, 0, read);
        }
        bis.close();
    }

重点:

ZipEntry其实就是即将要压缩文件的文件信息。
假设我们即将要压缩的是ggindex.jar文件,路径是根路径。
那么我们就要在ZipEntry对象中设置好。

ZipEntry zEntry = new ZipEntry(base + File.separator + file.getName());
zipOutput.putNextEntry(zEntry);

//根据上述的假设,我们就要这样写
ZipEntry zEntry = new ZipEntry("ggindex.jar");
zipOutput.putNextEntry(zEntry);//将其添加到压缩包中,准备进行压缩啦。

这里还要注意,ZipEntry参数base + File.separator + file.getName()是为了产生下面这样的效果。否则所有的文件都将会在根路径中。

这里写图片描述

解压

解压缩的关键就是通过ZipInputStream对象的getNextEntry()方法拿到ZipEntry。这个是压缩包里的压缩文件(条目)。

    public static void main(String[] args) throws Exception {

        String entryFile = "C:\\Users\\yutao\\Desktop\\pageage\\test.zip";

        File file = new File(entryFile);

        ZipInputStream zis = new ZipInputStream(new BufferedInputStream(new FileInputStream(file)));

        File fout = null;
        String parent = file.getParent();
        ZipEntry entry;
        while((entry = zis.getNextEntry())!=null && !entry.isDirectory()){
            fout = new File(parent, entry.getName());
            if(!fout.exists()){
                (new File(fout.getParent())).mkdirs();
            }

            BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(fout));

            int b = -1;
            byte[] buffer = new byte[1024];
            while((b = zis.read(buffer))!= -1){
                bos.write(buffer, 0, b);
            }
            bos.close();
        }

        zis.close();
    }

总结

知道javazip压缩原理后,其实还是很简单的!

①压缩时一定要用到ZipOutputStreamZipEntry,解压时,一定要用到ZipInputStreamZipEntry
②压缩时,既包含文件又包含目录时,基本上就要使用递归啦,路径一定要记得设置好。
③由于ZIP对每个文件进行单独压缩而没有利用文件间的冗余信息(即固实压缩),所以ZIP的压缩率会稍逊于tar压缩包。

参考地址:
http://www.imooc.com/wenda/detail/254317

http://www.cnblogs.com/lbangel/p/3459092.html

http://www.yiibai.com/java_io/java_io_zip_file.html

  • 5
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

山鬼谣me

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值