【问题标题】:Appending zip archive debugging附加 zip 存档调试
【发布时间】:2012-08-17 09:04:15
【问题描述】:

所以我对将文件附加到 zip 存档很感兴趣,我遇到了一些以前问过这个问题的用户,另一个用户给出了这个代码 sn-p 作为该问题的解决方案:

    public static void updateZip(File source, File[] files, String path){
    try{
        File tmpZip = File.createTempFile(source.getName(), null);
        tmpZip.delete();
        if(!source.renameTo(tmpZip)){
            throw new Exception("Could not make temp file (" + source.getName() + ")");
        }
        byte[] buffer = new byte[4096];
        ZipInputStream zin = new ZipInputStream(new FileInputStream(tmpZip));
        ZipOutputStream out = new ZipOutputStream(new FileOutputStream(source));
        for(int i = 0; i < files.length; i++){
            System.out.println(files[i].getName()+"being read");
            InputStream in = new FileInputStream(files[i]);
            out.putNextEntry(new ZipEntry(path + files[i].getName()));
            for(int read = in.read(buffer); read > -1; read = in.read(buffer)){
                out.write(buffer, 0, read);
            }
            out.closeEntry();
            in.close();
        }
        for(ZipEntry ze = zin.getNextEntry(); ze != null; ze = zin.getNextEntry()){
            if(!zipEntryMatch(ze.getName(), files, path)){
                out.putNextEntry(ze);
                for(int read = zin.read(buffer); read > -1; read = zin.read(buffer)){
                    out.write(buffer, 0, read);
                }
                out.closeEntry();
            }
        }
        out.close();
        tmpZip.delete();
    }catch(Exception e){
        e.printStackTrace();
    }
}

private static boolean zipEntryMatch(String zeName, File[] files, String path){
    for(int i = 0; i < files.length; i++){
        if((path + files[i].getName()).equals(zeName)){
            return true;
        }
    }
    return false;
}

我创建了一个小程序来测试这个方法,这是完成所有工作的方法:

    private static void appendArchive() {
    String filename = "foo";
    File[] filelist = new File[10];
    int i = 0;
    String temp = "";
    while (!filename.trim().equals("")) {
        System.out
                .println("Enter file names to add, then enter an empty line");
        filename = getInput();
        filelist[i] = new File(filename, filename);
        System.out.println("Adding " + filelist[i].getName());

    }
    System.out
            .println("What is the name of the zip archive you want to append");
    File zipSource = new File(getInput() + ".zip", "testZip.zip");
    try {
        Archiver.updateZip(zipSource, filelist, "");
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

每当我尝试运行这个程序时,我都会收到这个错误,然后是下一个:

java.lang.Exception: Could not make temp file (testZip.zip)

at Archiver.updateZip(Archiver.java:68)
at main.appendArchive(main.java:62)
at main.main(main.java:29)

我怀疑我传递的 zip 文件由于某种原因被认为是打开的,因此重命名方法在 Windows 上不起作用,所以我尝试使用您现在看到的 zip 文件的构造函数。我到底在做什么错在这里。我的测试输入是文件 2 和 2(附加到 2.zip )。它不应该是任何与目录相关的问题,因为文件是由程序生成的。

【问题讨论】:

  • 发布 IDE 的屏幕截图有什么意义?今后请不要这样做。如果它包含有用的技术信息,请复制/粘贴文本信息。
  • @AndrewThompson 我没有意识到它会将图片缩放到无法读取的大小。我主要为它在左窗格中显示的文件夹添加了图片,以表明我正在使用的文件位于默认工作目录中。我的错。

标签: java file exception zip append


【解决方案1】:

为我找到作品。我怀疑你可能想检查tmpZip.delete()的操作。

if (!tmpZip.exists() || tmpZip.delete()) {
    // ... Continue
} else {
    // ... File is locked
}

更新

我一直在玩代码,还有一些额外的缺陷......

将旧条目添加到新文件时,您使用的是现有的ZipEntry 条目。如果结果压缩不同,这将失败。您应该创建一个新的ZipEntry 并使用它来代替

ZipEntry ne = new ZipEntry(ze.getName());
out.putNextEntry(ne);
// Write bytes to file...
out.closeEntry();

你永远不会关闭ZipInputStream,这意味着tmpZip.delete()最后会失败。

你的错误处理几乎不存在......

ZipInputStream zin = null;
ZipOutputStream out = null;
try {
    // Append zip ...
} finally {
    try {
        zin.close();
    } catch (Exception exp) {
    }
    try {
        out.close();
    } catch (Exception exp) {
    }
}

将防止未来的文件锁定(我故意没有抓住IOException,因为我个人认为它应该被扔回被调用者)

在完成之前,您不应覆盖现有的 zip 文件。您应该为新的 zip 文件创建一个临时文件,将所有文件写入其中,附加现有文件,完成后,将现有文件替换为临时文件。

这意味着,如果出现问题,您并没有破坏现有的 zip 文件。

恕我直言

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-05
    • 1970-01-01
    • 2012-07-04
    • 2011-09-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多