android - 在 Android 中解压文件

标签 android unzip

当我尝试解压 zip 文件时出现以下错误

End-of-central-directory signature not found

我也尝试过 7zip lib,它在简单的 java 中运行良好,但在 android 平台上运行良好。
我收到“未找到相关 jar”错误。

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");
                } 

//文件头总是报错

最佳答案

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

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

关于android - 在 Android 中解压文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8295496/

相关文章:

php - 使用 PHP 解压缩较大的文件

javascript - 调整多个 Android 平板电脑屏幕的大小不起作用

java - 带有 ActionBar 的 TabHost fragment

android - 为 Qt for Android 目标构建 SMTP 客户端

vb.net - 在 VB.net 中解压文件

gradle - Gradle如何:从依赖项中解压缩ZIP文件中的ZIP文件,并保持增量生成功能

android InputManager 注入(inject)输入事件

android - 在android中使用JSON解析在gridview中显示图像

php - ZipArchive php 类以 root 身份提取文件。为什么?

python - 使用 Python 检查 zip 文件中是否存在目录