从ZIP压缩文件中删除文件,而无需在Java或Python中解压缩 [英] Delete files from a ZIP archive without Decompressing in Java or maybe Python

查看:522
本文介绍了从ZIP压缩文件中删除文件,而无需在Java或Python中解压缩的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从ZIP存档中删除文件而不使用Java(首选)或Python解压缩

Delete files from a ZIP archive without decompressing using Java (Preferred) or Python

您好,

我使用包含数百个高度压缩的文本文件的大型ZIP文件。当我解压缩ZIP文件时,它可能需要一段时间,并且很容易消耗多达20 GB的磁盘空间。我想从这些ZIP文件中删除某些文件,而不必仅解压缩和重新压缩我想要的文件。

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.

我更喜欢用Java做这个,但会考虑Python

I would prefer to do this in Java, but will consider Python

推荐答案

我在网上找到了这个

只有标准库的清洁解决方案,但我是不确定它是否包含在android sdk中,可以找到。

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");   
        } 
    }
}

这篇关于从ZIP压缩文件中删除文件,而无需在Java或Python中解压缩的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆