Zip异常:解压缩zip文件时未输入条目

时间:2015-09-08 13:45:39

标签: android unzip zipinputstream

我已经下载了zip文件,当我解压缩文件时,我得到了上述异常。 在解压缩我的zip文件后,下面是我的结构。

zip结构(解压缩后):       文件夹1        subfolder1              SUB1              SUB2        subfolder2             subf1             subf2

 String inputPath = Environment.getExternalStorageDirectory().getPath()+"/"+arrayListQuestions.get(0).getQuestion_asset_name();

            Log.e("inputPath",inputPath);
            String outputPath = Environment.getExternalStorageDirectory().getPath()+"/unzip/";
            Log.e("outPath",outputPath);
ZipManager zipManager = new ZipManager();
        try {
            zipManager.unzip(inputPath, outputPath);
        } catch (IOException e) {
            e.printStackTrace();
            Log.e("error_unzip",e.getLocalizedMessage());
        }

UnzipFunction:

public void unzip(String zipFilePath, String destDirectory) throws IOException {
        File destDir = new File(destDirectory);
        if (!destDir.exists()) {
            destDir.mkdir();
            }
        ZipInputStream zipIn = new ZipInputStream(new FileInputStream(zipFilePath));
        ZipEntry entry = zipIn.getNextEntry();
        // iterates over entries in the zip file
        while (entry != null) {
            String filePath = destDirectory + File.separator + entry.getName();
            if (!entry.isDirectory()) {
                // if the entry is a file, extracts it
                extractFile(zipIn, filePath);
                } else {
                // if the entry is a directory, make the directory
                File dir = new File(filePath);
                dir.mkdir();
                }
            zipIn.closeEntry();
            entry = zipIn.getNextEntry();
            }
        zipIn.close();
        }


    private void extractFile(ZipInputStream zipIn, String filePath) throws IOException {
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(filePath));
        byte[] bytesIn = new byte[BUFFER];
        int read = 0;
        while ((read = zipIn.read(bytesIn)) != -1) {
            bos.write(bytesIn, 0, read);
            }
        bos.close();
        }

1 个答案:

答案 0 :(得分:0)

解决方案是使用 ZipFile 而不是 ZipInputStreamZipFile 似乎可以优雅地处理无名 local file headers

注意 ZipFile 的缺点是它不能处理任意的 InputStream 并且只接受文件。

另请注意,该问题似乎是 Android 特有的。我在 Oracle 的 JRE 中没有遇到此异常。