我已经尝试了所有可能的方法并且我已经搜索了很多。我找到的唯一解决方案是这里 Adding (and overriding) files to compressed archive但是当我运行 zipFile.renameTo(tempFile) 行中的代码时,会发生一些事情,并且它会卡在那里。我不明白发生了什么?
有人可以帮助我吗?
File zipFile = new File("C:/ass.zip");
// get a temp file
File tempFile = null;
try {
tempFile = File.createTempFile(zipFile.getName(), null);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// delete it, otherwise you cannot rename your existing zip to it.
tempFile.delete();
System.out.println(zipFile.getAbsolutePath());
boolean renameOk=zipFile.renameTo(tempFile);
if (!renameOk)
{
try {
throw new Exception("could not rename the file "+zipFile.getAbsolutePath()+" to "+tempFile.getAbsolutePath());
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
byte[] buf = new byte[4096 * 1024];
ZipInputStream zin = null;
ZipEntry entry = null;
ZipOutputStream out = null;
try {
zin = new ZipInputStream(new FileInputStream(tempFile));
out = new ZipOutputStream(new FileOutputStream(zipFile));
entry = zin.getNextEntry();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
while (entry != null) {
String name = entry.getName();
if (entry.getName().contains("s.txt")) {
// Compress the files
InputStream in = null;
try {
in = new FileInputStream(entry.getName());
String fName = entry.getName();
// Add ZIP entry to output stream.
out.putNextEntry(new ZipEntry(fName));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// Transfer bytes from the file to the ZIP file
int len;
try {
while ((len = in.read(buf)) > 0) {
String s = new String(buf);
if (s.contains("host=")) {
buf = s.replaceAll("host=sssws", "host="+PropertyHandler.getInstance().getProperty("hostName")).getBytes();
}
out.write(buf, 0, (len < buf.length) ? len : buf.length);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// Complete the entry
try {
out.closeEntry();
in.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}else{
// Add ZIP entry to output stream.
try {
out.putNextEntry(new ZipEntry(name));
// Transfer bytes from the ZIP file to the output file
int len;
while ((len = zin.read(buf)) > 0) {
try {
out.write(buf, 0, len);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
try {
entry = zin.getNextEntry();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
// Close the streams
try {
zin.close();
// Complete the ZIP file
out.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
tempFile.delete();
}
最佳答案
你应该放置
tempFile.delete();
重命名 zipFile 后的这一行
boolean renameOk=zipFile.renameTo(tempFile);
所以就像这样
boolean renameOk=zipFile.renameTo(tempFile);
tempFile.delete();
关于Java 从现有 ZipFile 中删除条目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34177835/