解压缩.zip文件时,ZipInputStream getNextEntry为null

Oma*_*mar 5 java null zip file extract

我正在尝试提取.zip文件,我正在使用此代码:

String zipFile = Path + FileName;

FileInputStream fin = new FileInputStream(zipFile);
ZipInputStream zin = new ZipInputStream(fin);

ZipEntry ze = null;
while ((ze = zin.getNextEntry()) != null) {
    UnzipCounter++;
    if (ze.isDirectory()) {
        dirChecker(ze.getName());
    } else {
        FileOutputStream fout = new FileOutputStream(Path
                + ze.getName());
        while ((Unziplength = zin.read(Unzipbuffer)) > 0) {
            fout.write(Unzipbuffer, 0, Unziplength);                    
        }
        zin.closeEntry();
        fout.close();

    }
}
zin.close();
Run Code Online (Sandbox Code Playgroud)

但问题是,在调试时,当代码到达while(!= null)部分时,zin.getNextEntry()始终为null,因此它不会提取任何内容..
.zip文件是150kb ..我该如何修复这个?

.zip存在

我用来编写.zip的代码:

URL=intent.getStringExtra("DownloadService_URL");
    FileName=intent.getStringExtra("DownloadService_FILENAME");
    Path=intent.getStringExtra("DownloadService_PATH");
     File PathChecker = new File(Path);
    try{

    if(!PathChecker.isDirectory())
        PathChecker.mkdirs();

    URL url = new URL(URL);
    URLConnection conexion = url.openConnection();

    conexion.connect();
    int lenghtOfFile = conexion.getContentLength();
    lenghtOfFile/=100;

    InputStream input = new BufferedInputStream(url.openStream());
    OutputStream output = new FileOutputStream(Path+FileName);

    byte data[] = new byte[1024];
    long total = 0;

    int count = 0;
    while ((count = input.read(data)) != -1) {
        output.write(data, 0, count);
        total += count;

        notification.setLatestEventInfo(context, contentTitle, "???? ????? ??? " + FileName + " " + (total/lenghtOfFile), contentIntent);
        mNotificationManager.notify(1, notification);
    }


    output.flush();
    output.close();
    input.close();
Run Code Online (Sandbox Code Playgroud)

小智 6

当使用ZipInputStream读取zip文件时,可能会遇到以下问题:Zip文件包含序列中的条目和其他结构信息.此外,它们包含文件最末端(!)的所有条目的注册表.只有此注册表确实提供了有关正确的zip文件结构的完整信息.因此,通过使用流来读取序列中的zip文件有时会导致"猜测",这可能会失败.这是所有zip实现的常见问题,不仅适用于java.util.zip.更好的方法是使用ZipFile,它从文件末尾的注册表中确定结构.您可能需要阅读http://commons.apache.org/compress/zip.html,它会提供更多详细信息.


And*_*son 4

如果 Zip 放置在与此确切源相同的目录中(名为“91.zip”),则它可以正常工作。

import java.io.*;
import java.util.zip.*;

class Unzip {
    public static void main(String[] args) throws Exception {
        String Path = ".";
        String FileName = "91.zip";
        File zipFile = new File(Path, FileName);

        FileInputStream fin = new FileInputStream(zipFile);
        ZipInputStream zin = new ZipInputStream(fin);

        ZipEntry ze = null;
        int UnzipCounter = 0;
        while ((ze = zin.getNextEntry()) != null) {
            UnzipCounter++;
            //if (ze.isDirectory()) {
            //  dirChecker(ze.getName());
            //} else {
                byte[] Unzipbuffer = new byte[(int) pow(2, 16)];
                FileOutputStream fout = new FileOutputStream(
                    new File(Path, ze.getName()));
                int Unziplength = 0;
                while ((Unziplength = zin.read(Unzipbuffer)) > 0) {
                    fout.write(Unzipbuffer, 0, Unziplength);
                }
                zin.closeEntry();
                fout.close();
            //}
        }
        zin.close();
    }
}
Run Code Online (Sandbox Code Playgroud)

顺便提一句

  1. MP3 中的语言是什么,阿拉伯语?
  2. 我必须更改源代码才能编译它。
  3. 我使用File带有两个String参数的构造函数来自动插入正确的分隔符。