Java File Delete nio deleteEntriesFromZip(File zipFile, List entriesToDelete)

Here you can find the source of deleteEntriesFromZip(File zipFile, List entriesToDelete)

Description

Use this to delete some entries from a zip file.

License

Open Source License

Parameter

Parameter Description
zipFile a parameter
entriesToDelete a parameter

Exception

Parameter Description
IOException an exception

Declaration

public static void deleteEntriesFromZip(File zipFile, List<ZipEntry> entriesToDelete) throws IOException 

Method Source Code


//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();
    }
}

Related

  1. delete(File file)
  2. delete(File toRecurse)
  3. delete(final File directory, final FileFilter fileFilter)
  4. deleteAll(File f)
  5. deleteAll(File file)
  6. deleteFile(File f)
  7. deleteFile(File f)
  8. deleteFile(String fileName)
  9. deleteFile(String filename)