代码之家  ›  专栏  ›  技术社区  ›  Aditya

ZipException,同时以编程方式解压缩.zip

  •  0
  • Aditya  · 技术社区  · 7 年前

    我在试着减压 .zip ,在这段代码的帮助下以编程方式在我的应用程序中,

    public void unzip(String _zipFile, String _targetLocation) {
    
        dirChecker(_targetLocation);
    
        try {
            FileInputStream fin = new FileInputStream(_zipFile);
            ZipInputStream zin = new ZipInputStream(fin);
            ZipEntry ze = null;
    
            while ((ze = zin.getNextEntry()) != null) {
                if (ze.isDirectory()) {
                    dirChecker(ze.getName());
                } else {
                    FileOutputStream fout = new FileOutputStream(_targetLocation + ze.getName());
                    for (int c = zin.read(); c != -1; c = zin.read()) {
                        fout.write(c);
                    }
    
                    zin.closeEntry();
                    fout.close();
                }
            }
            zin.close();
    
            Log.i("xoxo", "unzip: completed!!!");
        } catch (Exception e) {
            Log.i("xoxo", "unzip: err: - " + e.toString());
        }
    }
    

    一切都很好,但问题是我犯了个错误,

    java.util.zip文件.ZipException:只有已压缩的条目才能有外部描述符

    在试图减压时 .zip文件 由运行在linux环境中的服务器创建。

    0 回复  |  直到 6 年前
        1
  •  0
  •   Ankit Dubey    6 年前

    你可以试试这个图书馆 https://github.com/ankitdubey021/ExtractionLib

    void extract(){
    
    try {
        File outputFile = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS), "value1.jar");
        Extract.extract(outputFile, new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS), "extration").toString());
    
        Toast.makeText(MainActivity.this, "Extracted!", Toast.LENGTH_SHORT).show();
    }catch(Exception e){
        System.out.println(e);
    }}
    

    它将很容易提取你的zip文件。三年前我在一个项目中用过它。还在工作。