首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在Android中解压缩文件

在Android中解压缩文件
EN

Stack Overflow用户
提问于 2011-11-28 12:00:50
回答 2查看 8.8K关注 0票数 2

当我试图解压缩文件时,出现了以下错误

中心端目录签名找不到

我还尝试了7zip lib,它在简单的java中运行得很好,但在android平台上却很好。

我得到了一个“依赖jar未找到”错误。

代码语言:javascript
复制
try {
            // Initiate the ZipFile
            ZipFile zipFile = new ZipFile(file);
            String destinationPath = destPath;

            // If zip file is password protected then set the password
            if (zipFile.isEncrypted()) {
                zipFile.setPassword(password);
            }

            //Get a list of FileHeader. FileHeader is the header information for all the
            //files in the ZipFile
            List fileHeaderList = zipFile.getFileHeaders();

            // Loop through all the fileHeaders
            for (int i = 0; i < fileHeaderList.size(); i++) {
                FileHeader fileHeader = (FileHeader)fileHeaderList.get(i);
                if (fileHeader != null) {

                    //Build the output file
                    String outFilePath = destinationPath + System.getProperty("file.separator") + fileHeader.getFileName();
                    File outFile = new File(outFilePath);

                    //Checks if the file is a directory
                    if (fileHeader.isDirectory()) {
                        //This functionality is up to your requirements
                        //For now I create the directory
                        outFile.mkdirs();
                        continue;
                    }

                    //Check if the directories(including parent directories)
                    //in the output file path exists
                    File parentDir = outFile.getParentFile();
                    if (!parentDir.exists()) {
                        parentDir.mkdirs();
                    }

                    //Get the InputStream from the ZipFile
                    is = zipFile.getInputStream(fileHeader);
                    //Initialize the output stream
                    os = new FileOutputStream(outFile);

                    int readLen = -1;
                    byte[] buff = new byte[BUFF_SIZE];

                    //Loop until End of File and write the contents to the output stream
                    while ((readLen = is.read(buff)) != -1) {
                        os.write(buff, 0, readLen);
                    }

                    //Please have a look into this method for some important comments
                    closeFileHandlers(is, os);

                    //To restore File attributes (ex: last modified file time, 
                    //read only flag, etc) of the extracted file, a utility class
                    //can be used as shown below
                    UnzipUtil.applyFileAttributes(fileHeader, outFile);

                    System.out.println("Done extracting: " + fileHeader.getFileName());
                } else {
                    System.err.println("fileheader is null. Shouldn't be here");
                } 

//文件头总是出现错误

EN

回答 2

Stack Overflow用户

发布于 2011-11-28 12:06:58

在Android中解压缩文件时,不需要第三方库。看看这个:

http://www.jondev.net/articles/Unzipping_Files_with_Android_%28Programmatically%29

票数 1
EN

Stack Overflow用户

发布于 2016-10-19 07:34:38

我想永远不会太晚,使用上面的解压真的太慢了,所以我修改了它。

使用下面的课程

代码语言:javascript
复制
    package com.example.epubdemo;

    import java.io.BufferedOutputStream;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.util.zip.ZipEntry;
    import java.util.zip.ZipInputStream;
    import android.util.Log;

    public class DecompressFast {



 private String _zipFile; 
  private String _location; 

  public DecompressFast(String zipFile, String location) { 
    _zipFile = zipFile; 
    _location = location; 

    _dirChecker(""); 
  } 

  public void unzip() { 
    try  { 
      FileInputStream fin = new FileInputStream(_zipFile); 
      ZipInputStream zin = new ZipInputStream(fin); 
      ZipEntry ze = null; 
      while ((ze = zin.getNextEntry()) != null) { 
        Log.v("Decompress", "Unzipping " + ze.getName()); 

        if(ze.isDirectory()) { 
          _dirChecker(ze.getName()); 
        } else { 
          FileOutputStream fout = new FileOutputStream(_location + ze.getName()); 
         BufferedOutputStream bufout = new BufferedOutputStream(fout);
          byte[] buffer = new byte[1024];
          int read = 0;
          while ((read = zin.read(buffer)) != -1) {
              bufout.write(buffer, 0, read);
          }




          bufout.close();

          zin.closeEntry(); 
          fout.close(); 
        } 

      } 
      zin.close(); 


      Log.d("Unzip", "Unzipping complete. path :  " +_location );
    } catch(Exception e) { 
      Log.e("Decompress", "unzip", e); 

      Log.d("Unzip", "Unzipping failed");
    } 

  } 

  private void _dirChecker(String dir) { 
    File f = new File(_location + dir); 

    if(!f.isDirectory()) { 
      f.mkdirs(); 
    } 
  } 


 }

用法

代码语言:javascript
复制
// save zip file in your internal storage directly not in any sub folder( if using any sub folder give below path correctly)
 String zipFile = Environment.getExternalStorageDirectory() + "/the_raven.zip"; //your zip name
  String unzipLocation = Environment.getExternalStorageDirectory() + "/unzippedtestNew/"; //your unzip directory name
DecompressFast df= new DecompressFast(zipFile, unzipLocation);
df.unzip();

不要忘记给读和写权限

代码语言:javascript
复制
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8295496

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档