覆盖ZipEntrys

时间:2011-02-24 11:16:00

标签: java stream zip overwrite

简单的问题,

我正在将一系列文本文件写入zip,只是将zipoutputstream中的fileoutputstream包装在一个printwriter中。

public static int saveData(File outfile, DataStructure input) {
//variables
ArrayList<String> out = null;
FileOutputStream fileout = null;
ZipOutputStream zipout = null;
PrintWriter printer = null;

//parameter tests

try {
    fileout = new FileOutputStream(outfile);
    zipout = new ZipOutputStream(fileout);
    printer = new PrintWriter(zipout);
} catch (Exception e) {
    e.printStackTrace();
    return util.FILE_INVALID;
}

for(DataItem data : input){
    //process the data into a list of strings

    try {
    zipout.putNextEntry(new ZipEntry( dataFileName ));
    for(String s : out) {
        printer.println(s);
    }
    zipout.closeEntry();
    } catch (Exception e) {
    try {
        fileout.close();
    } catch (Exception x) {
        x.printStackTrace();
        return util.CRITICAL_ERROR;
    }
    e.printStackTrace();
    return util.CRITICAL_ERROR;
    }

}


try {
    fileout.close();
} catch (Exception e) {
    e.printStackTrace();
    return util.CRITICAL_ERROR;
}

return util.SUCCESS;

}

以前在应用程序中我一直在开发我刚刚保存到当前目录进行测试,我知道在已经存在的文件的情况下该文件将被覆盖(并且已经利用了这个)。我不知道的是拉链的行为。它会覆盖同名的条目吗?或者它只是覆盖整个zip文件(这对我来说很方便。

K.Barad

2 个答案:

答案 0 :(得分:2)

如果您尝试添加重复的ZipEntry,您将收到异常。如果要替换当前条目,则需要将其删除并重新插入。我怀疑你获得的例外情况与this one非常相似。

答案 1 :(得分:2)

正如Joel所说,如果你尝试添加一个重复的ZipEntry,你将获得一个例外。如果要替换当前条目,则需要将其删除并重新插入。 您可能想要执行以下操作来实现它:

    private ZipFile addFileToExistingZip(File zipFile, File versionFile) throws IOException{
    // get a temp file
    File tempFile = File.createTempFile(zipFile.getName(), null);
    // delete it, otherwise you cannot rename your existing zip to it.
    tempFile.delete();

    boolean renameOk=zipFile.renameTo(tempFile);
    if (!renameOk)
    {
        throw new RuntimeException("could not rename the file "+zipFile.getAbsolutePath()+" to "+tempFile.getAbsolutePath());
    }
    byte[] buf = new byte[4096 * 1024];

    ZipInputStream zin = new ZipInputStream(new FileInputStream(tempFile));
    ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipFile));

    ZipEntry entry = zin.getNextEntry();
    while (entry != null) {
        String name = entry.getName();
        boolean toBeDeleted = false;
            if (versionFile.getName().indexOf(name) != -1) {
                toBeDeleted = true;
            }
        if(!toBeDeleted){
            // Add ZIP entry to output stream.
            out.putNextEntry(new ZipEntry(name));
            // Transfer bytes from the ZIP file to the output file
            int len;
            while ((len = zin.read(buf)) > 0) {
                out.write(buf, 0, len);
            }
        }
        entry = zin.getNextEntry();
    }
    // Close the streams
    zin.close();
    // Compress the files
    InputStream in = new FileInputStream(versionFile);
    String fName = versionFile.getName();
    // Add ZIP entry to output stream.
    out.putNextEntry(new ZipEntry(fName));
    // Transfer bytes from the file to the ZIP file
    int len;
    while ((len = in.read(buf)) > 0) {
        out.write(buf, 0, len);
    }
    // Complete the entry
    out.closeEntry();
    in.close();
    // Complete the ZIP file
    out.close();
    tempFile.delete();

    return new ZipFile(zipFile);
}

以上代码对我有用,需要在现有的zip文件中添加新的zip条目。如果条目已经存在于zip中,则覆盖它。 欢迎使用代码中的评论/改进! 谢谢!