使用JAVA将包含文件的文件夹添加到zip文件中
我使用java的java.util.zip api将文件和文件夹添加到zip文件中,但当我将多个文件添加到同一文件夹中时,它会删除旧内容。是否有任何方法可以在不修改文件夹中现有内容的情况下将文件添加到zip文件中?。 请帮忙,这很重要 这是我的示例代码:使用JAVA将包含文件的文件夹添加到zip文件中,java,zip,Java,Zip,我使用java的java.util.zip api将文件和文件夹添加到zip文件中,但当我将多个文件添加到同一文件夹中时,它会删除旧内容。是否有任何方法可以在不修改文件夹中现有内容的情况下将文件添加到zip文件中?。 请帮忙,这很重要 这是我的示例代码: ZipOutputStream zip = null; FileOutputStream fileWriter = null; fileWriter = new FileOutputStream(destZipFile); zip = new
ZipOutputStream zip = null;
FileOutputStream fileWriter = null;
fileWriter = new FileOutputStream(destZipFile);
zip = new ZipOutputStream(fileWriter);
zip.putNextEntry(new ZipEntry(destFilePath));
zip.write(content);
zip.flush();
zip.close();
如果要将新文件添加到现有zip文件中,必须首先解压缩所有文件,然后添加所有文件并再次压缩
请参阅以获取示例。我曾经找到过这个。。。它创建一个临时文件,并在添加额外文件之前将现有zip中的所有文件添加到“新”zip。如果两个文件具有相同的名称,则只添加“最新”的文件
public static void addFilesToExistingZip(File zipFile,
File[] files) 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[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) {
// 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
for (int i = 0; i < files.length; i++) {
InputStream in = new FileInputStream(files[i]);
// Add ZIP entry to output stream.
out.putNextEntry(new ZipEntry(files[i].getName()));
// 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();
}
publicstaticvoid将文件添加到现有zip(文件zipFile,
文件[]文件)引发IOException{
//获取临时文件
File tempFile=File.createTempFile(zipFile.getName(),null);
//删除它,否则无法将现有zip重命名为它。
tempFile.delete();
布尔renameOk=zipFile.renameTo(tempFile);
如果(!renameOk)
{
抛出新的RuntimeException(“无法将文件“+zipFile.getAbsolutePath()+”重命名为“+tempFile.getAbsolutePath()”);
}
字节[]buf=新字节[1024];
ZipInputStream zin=新的ZipInputStream(新文件输入流(tempFile));
ZipOutputStream out=newzipoutpstream(newfileoutputstream(zipFile));
ZipEntry entry=zin.getNextEntry();
while(条目!=null){
String name=entry.getName();
布尔notInFiles=true;
用于(文件f:文件){
如果(f.getName().equals(name)){
notInFiles=false;
打破
}
}
如果(未填充){
//将ZIP条目添加到输出流。
out.putNextEntry(新ZipEntry(名称));
//将字节从ZIP文件传输到输出文件
内伦;
而((len=zin.read(buf))>0){
out.write(buf,0,len);
}
}
entry=zin.getnextery();
}
//关闭溪流
zin.close();
//压缩文件
对于(int i=0;i0){
out.write(buf,0,len);
}
//完成条目
out.closeEntry();
in.close();
}
//完成ZIP文件
out.close();
tempFile.delete();
}
编辑:
我认为这已经超过2年了,所以可能有些东西已经不是最新的了。这是我的示例代码:ZipOutputStream zip=null;FileOutputStream fileWriter=null;fileWriter=新的FileOutputStream(destZipFile);zip=新的ZipoutStream(fileWriter);zip.putnextery(新ZipEntry(destFilePath));zip.write(内容);zip.flush();zip.close();当您同时调用
close()
时,不需要调用flush()
,因为close()
调用flush()
本身。