Java 如何读取zip文件的内容并保存文件

Java 如何读取zip文件的内容并保存文件,java,groovy,zipfile,Java,Groovy,Zipfile,我希望递归读取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

我希望递归读取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.txt
test2.jpg
存储到我的硬盘中

给你:

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)
        }
    }
}
import java.util.zip*
def zipIn=新文件('lol.zip')
def zip=新ZipFile(zipIn)
zip.entries().findAll{!it.directory}.each{e->
(e.name作为文件)。使用{f->
f、 父文件?.mkdirs()
f、 withOutputStream{w->
w使用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(); 
    }
   }    
}
导入java.io.File;
导入java.io.FileInputStream;
导入java.io.FileOutputStream;
导入java.io.IOException;
导入java.util.List;
导入java.util.zip.ZipEntry;
导入java.util.zip.ZipInputStream;
公共类解压
{
列表文件列表;
私有静态最终字符串输入\u ZIP\u FILE=“C:\\MyFile.ZIP”;
私有静态最终字符串输出\u FOLDER=“C:\\outputzip”;
公共静态void main(字符串[]args)
{
解压解压=新解压();
解压(输入文件,输出文件夹);
}
/**
*解开它
*@param zipFile输入zip文件
*@param output zip文件输出文件夹
*/
public void unZipIt(字符串zipFile、字符串outputFolder){
字节[]缓冲区=新字节[1024];
试一试{
//创建输出目录不存在
文件夹=新文件(输出文件夹);
如果(!folder.exists()){
folder.mkdir();
}
//获取zip文件内容
ZipInputStream zis=
新的ZipInputStream(新的FileInputStream(zipFile));
//获取压缩文件列表条目
ZipEntry ze=zis.getNextEntry();
while(ze!=null){
字符串文件名=ze.getName();
File newFile=新文件(outputFolder+File.separator+fileName);
System.out.println(“文件解压缩:+newFile.getAbsoluteFile());
//创建所有不存在的文件夹
//否则,您将点击压缩文件夹的FileNotFoundException
新文件(newFile.getParent()).mkdirs();
FileOutputStream fos=新FileOutputStream(新文件);
内伦;
而((len=zis.read(buffer))>0){
fos.写入(缓冲区,0,len);
}
fos.close();
ze=zis.getnextery();
}
zis.closeEntry();
zis.close();
系统输出打印项次(“完成”);
}捕获(IOEX异常){
例如printStackTrace();
}
}    
}

有两件事:首先,新文件(e.name)的父目录
文件可能不存在。其次,为什么用writer而不是InputStream来
?这不是假定字符数据吗?当涉及到父目录时,如果在处理zip的同一目录中运行脚本,它将存在。Steam已更改。zip条目名称不一定是一个路径元素深。在某些情况下条目的父目录需要在本地文件系统上创建,不幸的是,当您开始向文件写入内容时,这不会自动发生。您需要显式的
mkdirs()
call。是的,你完全正确,但我假设在编写脚本时没有子目录。请随意编辑帖子。@cfrick,哪里定义了
e