ZipUtil

package edu.abc.jythondemo.util;

import java.io.*;
import java.nio.charset.Charset;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipOutputStream;

public class ZipUtil {

    public static void main(String[] args) {
        //toZip("D:\\pythonfile\\add","D:\\pythonfile\\dest.zip");
        unZipFiles("D:\\pythonfile\\dest.zip","D:\\pythonfile\\");
    }

    public static void toZip(String srcDir,String destFile){
        FileOutputStream out=null;
        ZipOutputStream zos=null;
        try{
            File file = new File(destFile);
            if (!file.getParentFile().exists()){
                file.getParentFile().mkdirs();
            }else{
                if (file.exists()) {
                    file.delete();
                }

            }

            out=new FileOutputStream(file);
            zos=new ZipOutputStream(out);
            File sourceFile=new File(srcDir);
            if(!sourceFile.exists()){
                throw new RuntimeException(srcDir+"is not exits!");
            }
            compress(sourceFile,zos,sourceFile.getName());
        }catch(Exception e){
            e.printStackTrace();
        }finally {
            try {
                if(zos!=null) {
                    zos.close();
                }
                if(out!=null){
                    out.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    /*
    递归压缩
     */
    private static void compress(File sourceFile, ZipOutputStream zos, String name){
        byte[] buf=new byte[1024*1024];
        System.out.println("进行压缩文件名:"+sourceFile.getName());
        if(sourceFile.isFile()){
            System.out.println("file>>>"+sourceFile.getName());
            try {
                //向zip输出流中添加一个zip实体,构造器中name为zip实体的文件的名字
                zos.putNextEntry(new ZipEntry(name));
                //copy文件到zip输出流
                int len;
                FileInputStream in=new FileInputStream(sourceFile);
                while ((len=in.read(buf))!=-1){
                    zos.write(buf,0, len);
                }
                zos.closeEntry();
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }else{
            File[] listFiles=sourceFile.listFiles();
            if(listFiles==null||listFiles.length==0){
                System.out.println("空文件夹:"+name);
                System.out.println(sourceFile.getName());
                try {
                    zos.putNextEntry(new ZipEntry(name+"/"));
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }else{
                for (File file:listFiles){
                    compress(file,zos,name+"/"+file.getName());
                }
            }
        }
    }

    /*
    解压
     */
    public static void unZipFiles(String srcFilePath,String destDir){
        long start = System.currentTimeMillis();
        // 判断源文件是否存在
        File srcFile=new File(srcFilePath);
        if (!srcFile.exists()) {
            throw new RuntimeException(srcFile.getPath() + "所指文件不存在");
        }
        ZipFile zipFile=null;
        try {
            zipFile=new ZipFile(srcFile, Charset.forName("GBK"));
            Enumeration<?> entries = zipFile.entries();

            while(entries.hasMoreElements()){
                ZipEntry entry = (ZipEntry)entries.nextElement();
                System.out.println("解压" + entry.getName());
                // 如果是文件夹,就创建个文件夹
                if (entry.isDirectory()) {
                    String dirPath = destDir + "/" + entry.getName();
                    File dir = new File(dirPath);
                    dir.mkdirs();
                } else {
                    // 如果是文件,就先创建一个文件,然后用io流把内容copy过去
                    File targetFile = new File(destDir + "/" + entry.getName());
                    // 保证这个文件的父文件夹必须要存在
                    if(!targetFile.getParentFile().exists()){
                        targetFile.getParentFile().mkdirs();
                    }
                    targetFile.createNewFile();
                    // 将压缩文件内容写入到这个文件中
                    InputStream is = zipFile.getInputStream(entry);
                    FileOutputStream fos = new FileOutputStream(targetFile);
                    int len;
                    byte[] buf = new byte[1024*1024];
                    while ((len = is.read(buf)) != -1) {
                        fos.write(buf, 0, len);
                    }
                    // 关流顺序,先打开的后关闭
                    fos.close();
                    is.close();
                }
            }
            long end = System.currentTimeMillis();
            System.out.println("解压完成,耗时:" + (end - start) +" ms");
            srcFile.deleteOnExit();
        } catch (Exception e) {
            throw new RuntimeException("unzip error from ZipUtils", e);
        }finally {
            if(zipFile != null){
                try {
                    zipFile.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值