解压缩.zip文件时,ZipInputStream getNextEntry为null [英] ZipInputStream getNextEntry is null when extracting .zip files

查看:894
本文介绍了解压缩.zip文件时,ZipInputStream getNextEntry为null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to extract .zip files and I'm using this code:

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();

但问题是,在调试时,当代码到达while(!= null)部分时,zin.getNextEntry()始终为null,因此它不会提取任何内容。

.zip文件是150kb ..我该如何解决这个问题?

but the problem is that, while debugging, when the code reaches the while(!=null) part, the zin.getNextEntry() is always null so it doesnt extract anything..
The .zip file is 150kb.. How can I fix this?

.zip存在

我使用的代码是.zip:

Code I use to dl the .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();


推荐答案

如果Zip与此目录放在同一目录中确切的来源,名为91.zip,它的工作正常。

If the Zip is placed in the same directory as this exact source, named "91.zip", it works just fine.

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

BTW


  1. 那个MP3,阿拉伯语的语言是什么?

  2. 我不得不改变源代码以使其编译。

  3. 我使用文件构造函数,它接受两个 String 参数,自动插入正确的分隔符。

  1. what is the language in that MP3, Arabic?
  2. I had to alter the source to get it to compile.
  3. I used the File constructor that takes two String arguments, to insert the correct separator automatically.

这篇关于解压缩.zip文件时,ZipInputStream getNextEntry为null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆