【问题标题】:getting error while unzipping解压时出错
【发布时间】:2016-07-10 21:03:31
【问题描述】:

我的 UnZip 类不会解压缩整个文件。这个类是从另一个活动中调用的。我的 zip 文件保存在手机内部存储的主目录中。 zip 文件有文件夹和一些视频。 这个解压有什么问题? 我应该如何从 zip 中读取文件'解压缩和解压缩是相同的含义?

感谢您的帮助!

public class Unzip {
  private static final String INPUT_ZIP_FILE = "sdcard/downloaded_issue.zip";
  private static final String OUTPUT_FOLDER = "sdcard/Atlantis/";

  public static void main()
  {
    Unzip unZip = new Unzip();
    unZip.unZipIt(INPUT_ZIP_FILE, OUTPUT_FOLDER);
  }

/**
 * Unzip it
 * @param zipFile input zip file
 * @param outputFolder 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();
            if (ze.isDirectory()) {
            ze = zis.getNextEntry();
            }
        }

        zis.closeEntry();
        zis.close();

        System.out.println("Done");

    }catch(IOException ex){
        ex.printStackTrace();
    }
  }
}

【问题讨论】:

  • 这是哪种编程语言?乍一看像是 Java 或 C#。
  • 它是 Java。我正在用 Android Studio 编程
  • 然后标记它。如果包含编程语言,您将获得更快的响应;人们倾向于对此进行过滤。
  • 您建议使用哪个标签?
  • 看起来你做对了。我能做的不多了;我自己对java的zip系统不太熟悉,抱歉。

标签: java android-studio zip inputstream unzip


【解决方案1】:

我认为您的“while”循环已损坏;如果下一个条目是目录,您只会获取下一个条目,而我假设您可能正在尝试 skip 目录。

无论如何,由于您为遇到的所有文件创建了文件夹,因此您可以跳过文件夹条目并写入文件条目。唯一的例外是创建空文件夹。

用这段代码替换while循环应该可以:

    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
        if (ze.isDirectory()) {
            // create the folder
            newFile.mkdirs();
        }
        else {
            // create the parent folder and write to disk
            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();
        }
        // get the next item
        ze = zis.getNextEntry();
    }

【讨论】:

  • 我需要解压所有文件夹。不要跳过它们。
  • 不,等等。这不是递归的,除非文件夹为空,否则文件夹条目本身没有任何价值,因为无论如何您都会为文件自动创建文件夹。你能做的最好的就是创建它们然后跳过它们,是的。
  • @Weblu 编辑了答案。尽管在现实中唯一改变的是创建了空文件夹。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-09-14
  • 2018-02-16
  • 1970-01-01
  • 2014-04-13
  • 2014-02-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多