Delete files from a ZIP archive without decompressing using Java (Preferred) or Python
Hi,
I work with large ZIP files containing many hundreds of highly compressed text files. When I decompress the ZIP file it can take a while and easily consume up to 20 GB of diskspace. I would like to remove certain files from these ZIP files without having to decompress and recompress only the files I want.
Of course it is certainly possible to do this the long way, but very inefficient.
I would prefer to do this in Java, but will consider Python
I've found this on web
clean solution with only standard library, but I'm not sure whether it's included in android sdk, to be found.
import java.util.*;
import java.net.URI;
import java.nio.file.Path;
import java.nio.file.*;
import java.nio.file.StandardCopyOption;
public class ZPFSDelete {
public static void main(String [] args) throws Exception {
/* Define ZIP File System Properies in HashMap */
Map<String, String> zip_properties = new HashMap<>();
/* We want to read an existing ZIP File, so we set this to False */
zip_properties.put("create", "false");
/* Specify the path to the ZIP File that you want to read as a File System */
URI zip_disk = URI.create("jar:file:/my_zip_file.zip");
/* Create ZIP file System */
try (FileSystem zipfs = FileSystems.newFileSystem(zip_disk, zip_properties)) {
/* Get the Path inside ZIP File to delete the ZIP Entry */
Path pathInZipfile = zipfs.getPath("source.sql");
System.out.println("About to delete an entry from ZIP File" + pathInZipfile.toUri() );
/* Execute Delete */
Files.delete(pathInZipfile);
System.out.println("File successfully deleted");
}
}
}
I don't have code to do this, but the basic idea is simple and should translate into almost any language the same way. The ZIP file layout is just a series of blocks that represent files (a header followed by the compressed data), finished off with a central directory that just contains all the metadata. Here's the process:
See http://en.wikipedia.org/wiki/ZIP_%28file_format%29 for all the details on the ZIP file structures.
As bestsss suggests, you might want to perform the copying into another file, so as to prevent losing data in the event of a failure.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With