【问题标题】:How to read contents of zip file and save files如何读取 zip 文件的内容并保存文件
【发布时间】:2014-10-20 15:58:13
【问题描述】:

我希望递归读取 zip 文件的内容并将所有找到的文件保存到我的硬盘驱动器。

我正在阅读这样的 zip 文件:

def zipFile = new java.util.zip.ZipFile(new File('/Users/birdy/test.zip'))

zipFile.entries().findAll { !it.directory }.each {
    def is = zipFile.getInputStream(it)
    //how do i store this stream to a file?
}

如果 zip 文件包含以下文件:

folder1/test1.txt
folder2/test2.jpg

然后我希望将test1.txttest2.jpg 存储到我的HD

【问题讨论】:

    标签: java groovy zipfile


    【解决方案1】:

    给你:

    import java.util.zip.*
    
    def zipIn = new File('lol.zip')
    def zip = new ZipFile(zipIn)
    
    zip.entries().findAll { !it.directory }.each { e ->
        (e.name as File).with{ f ->
            f.parentFile?.mkdirs()
            f.withOutputStream { w ->
                w << zip.getInputStream(e)
            }
        }
    }
    

    一切都清楚了吗?

    【讨论】:

    • 有几件事:首先,new File(e.name) 文件的父目录可能不存在。第二,为什么withWriter 而不是withInputStream?那不是假设字符数据吗?
    • 当涉及到父目录时,如果您在处理 zip 的同一目录中运行脚本,它将存在。 Steam 已更改。
    • Zip 条目名称不一定是一个路径元素深度。在某些时候,需要在本地文件系统上创建条目的父目录,不幸的是,当您开始将内容写入文件时,这不会自动发生。你需要一个明确的mkdirs() 调用。
    • 是的,你说得对,但我假设在编写脚本时没有子目录。随意编辑帖子。
    • @cfrick,e 定义在哪里?
    【解决方案2】:

    使用 Java 代码

    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.util.List;
    import java.util.zip.ZipEntry;
    import java.util.zip.ZipInputStream;
    
    public class UnZip
    {
        List<String> fileList;
        private static final String INPUT_ZIP_FILE = "C:\\MyFile.zip";
        private static final String OUTPUT_FOLDER = "C:\\outputzip";
    
        public static void main( String[] args )
        {
            UnZip unZip = new UnZip();
            unZip.unZipIt(INPUT_ZIP_FILE,OUTPUT_FOLDER);
        }
    
        /**
         * Unzip it
         * @param zipFile input zip file
         * @param output zip file output folder
         */
        public void unZipIt(String zipFile, String outputFolder){
    
         byte[] buffer = new byte[1024];
    
         try{
    
            //create output directory is not exists
            File folder = new File(OUTPUT_FOLDER);
            if(!folder.exists()){
                folder.mkdir();
            }
    
            //get the zip file content
            ZipInputStream zis = 
                new ZipInputStream(new FileInputStream(zipFile));
            //get the zipped file list entry
            ZipEntry ze = zis.getNextEntry();
    
            while(ze!=null){
    
               String fileName = ze.getName();
               File newFile = new File(outputFolder + File.separator + fileName);
    
               System.out.println("file unzip : "+ newFile.getAbsoluteFile());
    
                //create all non exists folders
                //else you will hit FileNotFoundException for compressed folder
                new File(newFile.getParent()).mkdirs();
    
                FileOutputStream fos = new FileOutputStream(newFile);             
    
                int len;
                while ((len = zis.read(buffer)) > 0) {
                fos.write(buffer, 0, len);
                }
    
                fos.close();   
                ze = zis.getNextEntry();
            }
    
            zis.closeEntry();
            zis.close();
    
            System.out.println("Done");
    
        }catch(IOException ex){
           ex.printStackTrace(); 
        }
       }    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-06-28
      • 1970-01-01
      • 2020-06-16
      • 2012-02-26
      • 2013-03-18
      • 2016-09-30
      相关资源
      最近更新 更多