gpt4 book ai didi

java - 覆盖 ZipEntry

转载 作者:行者123 更新时间:2023-11-29 03:58:15 26 4
gpt4 key购买 nike

简单的问题

我正在将一系列文本文件写入 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com