java.util.zip能实现分卷压缩吗?查了一天也没查到相关的资料,有了解的指点下,谢谢。

解决方案 »

  1.   

    说下自己目前的解决方法。
    通过命令行调用解压缩软件进行分卷压缩。缺点是服务器必须安装解压缩软件。
    服务器安装的是linux系统,安装的是7zip,分卷压缩为zip文件。
    下面为代码片段,排版贴上来有点乱,见谅。/**
         * @param srcfolderPath 源文件,文件或文件夹路径
         * @param rarFilePath 压缩以后文件名
         * @param volumneSize 分卷压缩包大小,以M为单位
         * */
        public static void compress7zipFile(String srcFilePath,String targetZipName,int volumneSize) throws IOException{
            getFiles(srcFilePath);
            File listFile = File.createTempFile("fileList", ".tmp");
    FileOutputStream fs = new FileOutputStream(listFile);
    try {
    StringBuffer fileList = new StringBuffer();
    //拼接要打包的文件路径
    for(String filePath:filePaths){
                fileList.append(filePath+"\n");
            }  
    fs.write(fileList.toString().getBytes());
    fs.flush();
    //使用Linux系统下命令将文件夹分卷压缩为zip文件,在windows系统下使用解压缩工具可直接解压
    String command = "7za a "+ targetZipName+" "+ srcFilePath +" -v" +volumneSize +"m";
    new CompressThread(command).start();
    } catch (Exception e) {
    e.printStackTrace();
    }finally{
    fs.close();
    }
    }
    public static void getFiles(String path) {
     File dir = new File(path);     //根据文件地址创建File对象
     File files[] = dir.listFiles();  //获取文件夹下的文件数组
     for (File file:files ) { //循环遍历数组
       if (file.isDirectory())   //判断文件是否是一个目录
           getFiles(file.getAbsolutePath()); //如果为文件夹,继续执行本方法
       else {
            filePaths.add(file.getAbsolutePath()); //将文件路径添加到集合中
           }
       }
    }

    private static final class CompressThread extends Thread {
    private final String command;
    private CompressThread(String command){
    this.command = command;

    public void run(){
        try {
    Runtime runtime = Runtime.getRuntime();
    final Process process =  runtime.exec(command.toString()+"\n");
    process.getOutputStream().close();
    } catch (Exception e) {
    e.printStackTrace();
    }
    }
    }