java - 覆盖 ZipEntry

标签 java stream zip overwrite

简单的问题

我正在将一系列文本文件写入 zip,只需将文件输出流包装在 zipoutputstream 中,然后包装在 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 的行为。它会覆盖同名的条目吗?或者它会简单地覆盖整个 zip 文件(这对我的目的来说很方便。

K.巴拉德

最佳答案

正如 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 中,则覆盖它。 欢迎对代码提出评论/改进! 谢谢!

关于java - 覆盖 ZipEntry,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5103881/

相关文章:

java - 使用 action_view Intent 后 Android 应用程序崩溃

javascript - 流与可观察对象

Azure Blob 存储流性能问题

linux - 使用 xz 而不是 gz - 非常慢

java - 类级别未使用的传递依赖分析

java - 将大小设置为 TreeSet

java - 如何通过 Qt JNI 将 null 从 C++ 传递到 Java?

java:创建zip文件时刷新流

Powershell ZIP CopyHere 抵消异步行为

java - 使用 util.zip 压缩文件 无目录