关于 java的 zip 代码备忘

关于 java的 zip 代码备忘

关于 zip 的代码备忘, 已在 osx 环境下自测。 百度发现一个据说很有效率的代码,测一下发现跟自己想要的有点不一样, 拿来改改。
原文:http://ask.zol.com.cn/x/5269802.html 里面的yanfugen包你满意那个回答

上代码

package com.primeton.etc.tools;

import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class TestFile {
    static int BUFF_SIZE = 4096;

    public static void main(String[] args) {
       

        zip("/Users/zengyujie","local","/Users/zengyujie/local.zip");


    }

    public static void zip(String inputFilePath,String inputFileName, String outputZipFullFileName){
        long begin = System.currentTimeMillis();
        FileOutputStream out = null;
        BufferedOutputStream buffOut = null;
        ZipOutputStream zout = null;
        try {
            out = new FileOutputStream(outputZipFullFileName);
            buffOut = new BufferedOutputStream(out);
            zout = new ZipOutputStream(buffOut);
            zip(new File(inputFilePath+"/"+inputFileName),inputFileName, zout);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            closeStream(zout);
            closeStream(buffOut);
            closeStream(out);
        }

        long end = System.currentTimeMillis();
        System.out.println("生成压缩文件:"+outputZipFullFileName+",用时:" + (end - begin) + " ms");
    }

    private static void zip(File file, String name ,ZipOutputStream zout) {
//如果files长度为0,zout.close()方法会抛异常: ZIP file must have at least one entry
        if (!file.exists()) {
            return;
        }

        if (file.isDirectory()) {
            File[] listFiles = file.listFiles();
            if(listFiles == null || listFiles.length == 0){
                // 空文件夹的处理
                try {
                    zout.putNextEntry(new ZipEntry(name+"/"));
                    // 没有文件,不需要文件的copy
                    zout.closeEntry();
                }catch (Exception e){
                    e.printStackTrace();
                }finally {

                }


            }else{
                for (File temp : listFiles) {
                    zip(temp,name+"/"+temp.getName(),zout);
                }
            }

        } else {
            InputStream in = null;
            BufferedInputStream buffIn = null;
            try {
                in = new FileInputStream(file);
                buffIn = new BufferedInputStream(in, BUFF_SIZE);
                ZipEntry zipEntry = new ZipEntry(name);
                zout.putNextEntry(zipEntry);
                int len = 0;
                byte data[] = new byte[BUFF_SIZE];
                while ((len = buffIn.read(data)) != -1) {
                    zout.write(data, 0, len);
                }
            } catch (Exception e) {
                e.printStackTrace();
                return;
            } finally {
                try {
                    zout.closeEntry();
                } catch (IOException e) {
//                        LogUtils.exception(e, "Close zip file entry failed");
                }
                closeStream(buffIn);
                closeStream(in);
            }
        }


    }


    private static void closeStream(Closeable o) {
        if (o != null)
            try {
                o.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
    }


}

后面发现对于大文件处理,效率还是太低。
于是直接调用 linux 的 zip 命令进行压缩

import java.io.*;
import java.nio.charset.Charset;

public class TestRunTime {

    public static String runCmd(String cmd,String cmdPath) {
        String result = "";
        File execFilePath = new File(cmdPath);
        BufferedReader br=null;
        InputStreamReader is=null;
        try {

            String[] execCmd= {"/bin/sh","-c",cmd};

            Process ps = Runtime.getRuntime().exec(execCmd, null,execFilePath);
            is = new InputStreamReader(ps.getInputStream(), Charset.forName("UTF-8"));
            br = new BufferedReader(is);
            String line ;
            while ((line = br.readLine()) != null) {
                System.out.println(line);
                result += line + "\n";
            }
//            Thread.sleep(5000);
            is.close();
            br.close();
            System.out.println("close ... ");
            ps.waitFor();
            System.out.println("wait over ...");

            return result;
        } catch (IOException ioe) {
            ioe.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }finally {
            closeStream(is);
            closeStream(br);
        }
        return null;

    }


    public static void main(String[] args) {
        String outputFilePath = "/Users/zengyujie/Upload";
        String outputFileName = "123.zip";
        String inputFile = "test.txt";
        String inputFilePath = "/Users/zengyujie/local/";

        File outFile = new File(outputFilePath);
        if (!outFile.exists()){
            outFile.mkdirs();
        }
        System.out.println(System.getProperty("os.name"));
        String cmd;
        //windows 不适用
        if(!System.getProperty("os.name").startsWith("W")) {
            //压缩文件
//             cmd = "zip " + outputFilePath + "/" + outputFileName + " " + inputFile;

             //压缩目录  -r 递归包含子目录下所有文件  -q 忽略zip日志打印
            cmd = "zip -r -q " + outputFilePath + "/" + outputFileName + " * " ;
            runCmd(cmd,inputFilePath);
        }else{
            cmd="";
        }

        System.out.println(111);
    }

    private static void closeStream(Closeable o) {
        if (o != null)
            try {
                o.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值