我想知道是否有一种方法,而不仅仅是将文件添加到 zip,而是在添加文件时重命名文件。我知道写方法:
(while ((len = zin.read(buf)) > 0) {
out.write(buf, 0, len);
}
我在整个互联网上搜索过,没有关于它的信息。Java有没有类es或方法?我不想重命名然后添加它的唯一原因是因为我正在使用 aux 文件处理 Windows。我的整个代码是:
File tempFile = File.createTempFile(zipFile.getName(), null);
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[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 notInFiles = true;
for (File f : files) {
if (f.getName().equals(name)) {
notInFiles = false;
break;
}
}
if (notInFiles) {
out.putNextEntry(new ZipEntry(name));
int len;
while ((len = zin.read(buf)) > 0) {
out.write(buf, 17, len);
}
}
entry = zin.getNextEntry();
}
zin.close();
for (int i = 0; i < files.length; i++) {
InputStream in = new FileInputStream(files[i]);
out.putNextEntry(new ZipEntry(files[i].getName()));
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
out.closeEntry();
in.close();
}
out.close();
tempFile.delete();