2

简单的问题,

我正在将一系列文本文件写入一个 zip,只需将一个文件输出流包装在一个 zipoutputstream 中,然后在一个打印机中。

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 文件(这对我的目的来说很方便。

巴拉德

4

2 回答 2

4

正如乔尔所说,如果您尝试添加重复的 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 中,则覆盖它。欢迎对代码进行评论/改进!谢谢!

于 2012-08-07T19:46:47.483 回答
2

如果您尝试添加重复的 ZipEntry,您将收到异常。如果要替换当前条目,则需要将其删除并重新插入。我怀疑你得到的异常和这个差不多

于 2011-02-24T12:43:35.173 回答