ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

zip压缩工具类

2019-03-30 18:48:57  阅读:253  来源: 互联网

标签:压缩工具 zip sourceFile 文件夹 zipFileName new out


 

java将有关zip压缩的内容都封装在java.util.zip宝中,用java实现zip压缩,不用考虑压缩算法,java已经将这些进行了封装

实际上用java实现zip压缩涉及的就是一个“输入输出流”的概念

用java实现一个文件的zip压缩,过程可以简单地表示为:

 

当然具体实现要比这个复杂一点,比如要先像zip文件中写入目录进入点。。如果要压缩文件夹中的内容要遍历文件夹中的文件和子文件夹。

/**
 * @author: hxp
 * @date: 2019/3/30 18:09
 * @description:zip压缩工具
 */
public class ZipCompress {
    /**
     * 目的地Zip文件
     */
    private String zipFileName;
    /**
     * 源文件(待压缩的文件或文件夹)
     */
    private String sourceFileName;

    public ZipCompress(String zipFileName,String sourceFileName)
    {
        this.zipFileName=zipFileName;
        this.sourceFileName=sourceFileName;
    }

    public void zip() throws Exception
    {
        //File zipFile = new File(zipFileName);
        System.out.println("压缩中...");

        //创建zip输出流
        ZipOutputStream out = new ZipOutputStream( new FileOutputStream(zipFileName));

        //创建缓冲输出流
        BufferedOutputStream bos = new BufferedOutputStream(out);

        File sourceFile = new File(sourceFileName);

        //调用函数
        compress(out,bos,sourceFile,sourceFile.getName());

        bos.close();
        out.close();
        System.out.println("压缩完成");

    }

    public void compress(ZipOutputStream out,BufferedOutputStream bos,File sourceFile,String base) throws Exception
    {
        //如果路径为目录(文件夹)
        if(sourceFile.isDirectory())
        {

            //取出文件夹中的文件(或子文件夹)
            File[] list = sourceFile.listFiles();

            //如果文件夹为空,则只需在目的地zip文件中写入一个目录进入点
            if(list.length==0)
            {
                System.out.println(base+"/");
                out.putNextEntry(  new ZipEntry(base+"/") );
            }
            else//如果文件夹不为空,则递归调用compress,文件夹中的每一个文件(或文件夹)进行压缩
            {
                for(int i=0;i<list.length;i++)
                {
                    compress(out,bos,list[i],base+"/"+list[i].getName());
                }
            }
        }
        else//如果不是目录(文件夹),即为文件,则先写入目录进入点,之后将文件写入zip文件中
        {
            out.putNextEntry( new ZipEntry(base) );
            FileInputStream fos = new FileInputStream(sourceFile);
            BufferedInputStream bis = new BufferedInputStream(fos);

            int tag;
            System.out.println(base);
            //将源文件写入到zip文件中
            while((tag=bis.read())!=-1)
            {
                bos.write(tag);
            }
            bis.close();
            fos.close();

        }
    }
}

 

标签:压缩工具,zip,sourceFile,文件夹,zipFileName,new,out
来源: https://www.cnblogs.com/h-c-g/p/10628458.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有