Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/399.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java:合并2个Zip文件_Java_Merge_Copy_Zip_Append - Fatal编程技术网

Java:合并2个Zip文件

Java:合并2个Zip文件,java,merge,copy,zip,append,Java,Merge,Copy,Zip,Append,我目前正在开发一个程序,在某一点上,它必须合并两个.zip文件(originalZip和newZip应该合并到moddedZip,newZip覆盖文件,如果它们都存在的话)。因为我以前的任何项目都没有使用.zip文件,所以我在谷歌上搜索了一段时间,直到找到了。虽然它只是附加单个文件,但我想我可以用它来合并文件 这就是我现在拥有的: import java.io.FileOutputStream; import java.io.IOException; import java.io.InputSt

我目前正在开发一个程序,在某一点上,它必须合并两个.zip文件(originalZip和newZip应该合并到moddedZip,newZip覆盖文件,如果它们都存在的话)。因为我以前的任何项目都没有使用.zip文件,所以我在谷歌上搜索了一段时间,直到找到了。虽然它只是附加单个文件,但我想我可以用它来合并文件

这就是我现在拥有的:

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipOutputStream;

public class ModZip {   
    // 4MB buffer
    private static final byte[] BUFFER = new byte[4096 * 1024];

    // copy input to output stream   
    public static void copy(InputStream input, OutputStream output) throws IOException {
        int bytesRead;
        while ((bytesRead = input.read(BUFFER))!= -1) {
            output.write(BUFFER, 0, bytesRead);
        }
    }

    public static void patch(String path) throws Exception {
        // read the original zip
        ZipFile originalZip = new ZipFile(path);
        
        
        // write the modded zip with new Name
        ZipOutputStream moddedZip = new ZipOutputStream(new FileOutputStream(path.substring(0, (path.length()-4))+"-modded.zip"));

        // copy contents from original zip to the modded zip
        Enumeration<? extends ZipEntry> entries = originalZip.entries();
        while (entries.hasMoreElements()) {
            ZipEntry e = entries.nextElement();
            System.out.println("copy: " + e.getName());
            moddedZip.putNextEntry(e);
            System.out.println("putnextEntry done");
            if (!e.isDirectory()) {
                copy(originalZip.getInputStream(e), moddedZip);
            }
            moddedZip.closeEntry();
        }

        // replace the original zip-files with new ones       
        ZipFile newZip = new ZipFile("/Users/user/Desktop/NEW.zip");
        Enumeration<? extends ZipEntry> newentries = newZip.entries();
        System.out.println(newentries);
        while (newentries.hasMoreElements()) {
            ZipEntry e = newentries.nextElement();
            System.out.println("append: " + e.getName());
            moddedZip.putNextEntry(e);
            System.out.println("putnextEntry done");
            if (!e.isDirectory()) {
                copy(newZip.getInputStream(e), moddedZip);
            }
            moddedZip.closeEntry();
        }
        
        System.out.println("appending done ");

        // close
        originalZip.close();
        newZip.close();
        moddedZip.close();
        System.out.println("all done");
    }
}
import java.io.FileOutputStream;
导入java.io.IOException;
导入java.io.InputStream;
导入java.io.OutputStream;
导入java.util.zip.ZipEntry;
导入java.util.zip.ZipFile;
导入java.util.zip.ZipoutStream;
公共类ModZip{
//4MB缓冲区
私有静态最终字节[]缓冲区=新字节[4096*1024];
//将输入复制到输出流
公共静态无效复制(InputStream输入、OutputStream输出)引发IOException{
int字节读取;
而((bytesRead=input.read(BUFFER))!=-1){
输出写入(缓冲区,0,字节读取);
}
}
公共静态无效修补程序(字符串路径)引发异常{
//读一下原稿
ZipFile originalZip=新ZipFile(路径);
//用新名称编写修改过的zip
ZipOutputStream moddedZip=新的zipoutpstream(新文件输出流(path.substring(0,(path.length()-4))+“-moded.zip”);
//将内容从原始zip复制到修改过的zip

枚举你在哪里调用该方法?我的ui调用该方法,给它一个路径:zip.patch(getPath());私有字符串getPath(){return pathInput.getText().trim();}显示代码,特别是如何处理异常。试试{zip.patch(getPath());}catch(Exception x){}“我没有得到任何例外,”使用
试试{zip.patch(getPath();}catch(Exception x){-->x.printStackTrace();
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipOutputStream;

public class ModZip {   
    // 4MB buffer
    private static final byte[] BUFFER = new byte[4096 * 1024];

    // copy input to output stream   
    public static void copy(InputStream input, OutputStream output) throws IOException {
        int bytesRead;
        while ((bytesRead = input.read(BUFFER))!= -1) {
            output.write(BUFFER, 0, bytesRead);
        }
    }

    public static void patch(String path) throws Exception {
        // read the zips
        ZipFile originalZip = new ZipFile(path);
        ZipFile newZip = new ZipFile("/Users/user/Desktop/NEW.zip");


        // write the modded zip with new Name
        ZipOutputStream moddedZip = new ZipOutputStream(new FileOutputStream(path.substring(0, (path.length()-4))+"-modded.zip"));

        // copy contents from original zip to the modded zip
        Enumeration<? extends ZipEntry> entries = originalZip.entries();
        while (entries.hasMoreElements()) {
            ZipEntry e = entries.nextElement();
            
            String name = e.getName();
            if(newZip.getEntry(name) == null) {
                System.out.println("copy: " + e.getName());
                moddedZip.putNextEntry(e);
                System.out.println("putnextEntry done");
                if (!e.isDirectory()) {
                    copy(originalZip.getInputStream(e), moddedZip);
                }
                moddedZip.closeEntry();
            }
        }

        // replace the original zip-files with new ones       
        
        Enumeration<? extends ZipEntry> newentries = newZip.entries();
        System.out.println(newentries);
        while (newentries.hasMoreElements()) {
            ZipEntry e = newentries.nextElement();
            System.out.println("append: " + e.getName());
            moddedZip.putNextEntry(e);
            System.out.println("putnextEntry done");
            if (!e.isDirectory()) {
                copy(newZip.getInputStream(e), moddedZip);
            }
            moddedZip.closeEntry();
        }

        System.out.println("appending done ");

        // close
        originalZip.close();
        newZip.close();
        moddedZip.close();
        System.out.println("all done");
    }
}