【问题标题】:Unzip a zipped folder with sub folder structure into a single folder将具有子文件夹结构的压缩文件夹解压缩到单个文件夹中
【发布时间】:2014-04-18 09:43:52
【问题描述】:

我想将具有子文件夹结构的压缩文件夹解压缩到一个文件中,而不是同一个子文件夹形式。我有以下代码,它创建 zip 的子文件夹结构并复制文件。但我需要将所有文件复制到一个文件夹中。有没有可能。

下面是代码:- 公共类解压 { 列表文件列表; 私有静态最终字符串 INPUT_ZIP_FILE = "C:\MyFile.zip"; 私有静态最终字符串 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 unzip


    【解决方案1】:

    拿走这个

    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.util.zip.ZipEntry;
    import java.util.zip.ZipInputStream;
    
    public class Unzip {
    
        public static final String OUTPUT_FOLDER = "/tmp/zipoutput";
        public static final String INPUT_ZIP_FILE = "/tmp/ziptest.zip";
    
        public static void main(String[] args) {
            UnZip unZip = new UnZip();
            unZip.unZipIt(INPUT_ZIP_FILE, OUTPUT_FOLDER);
        }
    
        public static class UnZip {
    
            /**
             * 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(outputFolder);
                    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();
                        if (ze.isDirectory()) {
                            ze = zis.getNextEntry();
                            continue;
                        }
                        System.out.println("origin file: " + fileName);
                        fileName = new File(fileName).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();
                }
            }
        }
    }
    

    【讨论】:

    • 我试过了,但它抛出“FileNotFoundException”。解压缩时是否需要与压缩文件夹中相同的子文件夹结构?
    • @user3365075 不 - 您必须将 INPUT_ZIP_FILEOUTPUT_FOLDER 更改为您自己的环境。
    • 对不起,我错过了一行。它部分工作正常,只获取子文件夹外的文件,而不是子文件夹内的文件。
    • @user3365075 两者都需要。你能做一个只包含文件的小例子吗 - 一个在子文件夹中,一个在外部。之后你可以把程序生成的输出origin file:... file unzip :...发给我
    • 我的机器出了点问题...你建议的代码已经解决了...:)