Here you can find the source of deleteEntriesFromZip(File zipFile, List
Parameter | Description |
---|---|
zipFile | a parameter |
entriesToDelete | a parameter |
Parameter | Description |
---|---|
IOException | an exception |
public static void deleteEntriesFromZip(File zipFile, List<ZipEntry> entriesToDelete) throws IOException
//package com.java2s; //License from project: Open Source License import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.nio.file.Files; import java.nio.file.StandardCopyOption; import java.util.List; import java.util.zip.ZipEntry; import java.util.zip.ZipInputStream; import java.util.zip.ZipOutputStream; public class Main { /**//w ww. j av a 2 s .co m * Use this to delete some entries from a zip file. WARNING!! This method does NOT * use very advanced string matching. Check the source before using. * @param zipFile * @param entriesToDelete * @throws IOException */ public static void deleteEntriesFromZip(File zipFile, List<ZipEntry> entriesToDelete) throws IOException { byte[] buffer = new byte[1024 * 32]; File newZipFile = new File(zipFile.getParentFile(), zipFile.getName() + ".tmp"); if (newZipFile.exists()) { newZipFile.delete(); } ZipOutputStream zos = null; ZipInputStream zis = null; try { zos = new ZipOutputStream(new FileOutputStream(newZipFile)); zis = new ZipInputStream(new FileInputStream(zipFile)); ZipEntry entry = zis.getNextEntry(); while (entry != null) { boolean shouldCopy = true; for (ZipEntry toCheck : entriesToDelete) { if (entry.getName().contains(toCheck.getName())) { shouldCopy = false; } } if (shouldCopy) { zos.putNextEntry(entry); int len; while ((len = zis.read(buffer)) > 0) { zos.write(buffer, 0, len); } } entry = zis.getNextEntry(); } } finally { zos.closeEntry(); zos.close(); zis.closeEntry(); zis.close(); } //Replace the old zip file Files.copy(newZipFile.toPath(), zipFile.toPath(), StandardCopyOption.REPLACE_EXISTING); //delete temporary newZipFile.delete(); } }