2

簡単な質問、

一連のテキストファイルをzipに書き込んでおり、fileoutputstreamを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.バラッド

4

2 に答える 2

4

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内に存在する場合は、上書きします。コードのコメント/改善は大歓迎です!ありがとう!

于 2012-08-07T19:46:47.483 に答える
2

重複するZipEntryを追加しようとすると、例外が発生します。現在のエントリを置き換える場合は、エントリを削除して再挿入する必要があります。あなたが得る例外はこれとほとんど同じだと思います。

于 2011-02-24T12:43:35.173 に答える