2011-09-26 430 views
4

,我使用此代码为null while(!= null)部分,zin.getNextEntry()始终为空,因此它不会提取任何东西。
.zip文件是150kb ..我该如何解决这个问题?ZipInputStream getNextEntry解压时的.zip我试图提取.zip文件的文件

该.zip存在我使用中的DL的.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(); 
+0

听起来像是'zipFile'可能不存在。仔细检查'Path'和'FileName'以确保它们是正确的。 – bamana

+0

@bamana完全不可能使用该代码,它会在新的FileInputStream中抛出IOException。 – EJP

+0

请在发布到公共论坛的源代码中使用camelCase作为属性名称。 –

回答

3

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

BTW

  1. 是什么在MP3,阿拉伯语的语言?我不得不改变源代码来编译它。
  2. 我使用了File构造函数,它需要两个String参数来自动插入正确的分隔符。
+0

关于第一个问题。是的,它的阿拉伯语和古兰经。 – Omar

+0

@Andrew Thompson:http://stackoverflow.com/a/13911232/1495442 – Ria

0

此代码工作正常,我。也许你需要检查zipFile字符串是否有效?

String zipFile = "C:/my.zip"; 

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

    ZipEntry ze = null; 
    while ((ze = zin.getNextEntry()) != null) { 
     System.out.println("got entry " + ze); 
    } 
    zin.close(); 

在3.3Mb zip文件上产生有效结果。

0

此代码似乎对我来说正常工作。

你确定你的zip文件是一个有效的zip文件吗?如果文件不存在或不可读,那么你将得到一个FileNotFoundException,但是如果该文件是空的或不是有效的zip文件,那么你将得到ze == null。

while ((ze = zin.getNextEntry()) != null) { 

您指定的zip不是有效的zip文件。入口的大小是4294967295

while ((ze = zin.getNextEntry()) != null) { 
    System.out.println("ze=" + ze.getName() + " " + ze.getSize()); 
    UnzipCounter++; 

这给:

ze=595.mp3 4294967295 
... 
Exception in thread "main" java.util.zip.ZipException: invalid entry size (expected 4294967295 but got 341297 bytes) 
    at java.util.zip.ZipInputStream.readEnd(ZipInputStream.java:386) 
    at java.util.zip.ZipInputStream.read(ZipInputStream.java:156) 
    at java.io.FilterInputStream.read(FilterInputStream.java:90) 
    at uk.co.farwell.stackoverflow.ZipTest.main(ZipTest.java:29) 

一个有效的zip文件试试你的代码。

+0

正如你所说,该文件明显存在,因为它会抛出异常..我只是redownloaded zip,用Android应用程序(FileExpert)打开,并成功提取.zip!所以.zip的作品! – Omar

+0

*“我只是redownload拉链..”* 1)网络是一个大的地方。小心分享一个网址? 2)Java只理解某些类型的Zip压缩。 –

+0

http://www.tinyurl.com/5udabdl ..我用我用于.zip的代码编辑了问题..我用C#应用压缩.zip文件我写了 – Omar

3

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

1

试试这个代码: -

private boolean extractZip(String pathOfZip,String pathToExtract) 
{ 


     int BUFFER_SIZE = 1024; 
     int size; 
     byte[] buffer = new byte[BUFFER_SIZE]; 


     try { 
      File f = new File(pathToExtract); 
      if(!f.isDirectory()) { 
       f.mkdirs(); 
      } 
      ZipInputStream zin = new ZipInputStream(new BufferedInputStream(new FileInputStream(pathOfZip), BUFFER_SIZE)); 
      try { 
       ZipEntry ze = null; 
       while ((ze = zin.getNextEntry()) != null) { 
        String path = pathToExtract +"/"+ ze.getName(); 

        if (ze.isDirectory()) { 
         File unzipFile = new File(path); 
         if(!unzipFile.isDirectory()) { 
          unzipFile.mkdirs(); 
         } 
        } 
        else { 
         FileOutputStream out = new FileOutputStream(path, false); 
         BufferedOutputStream fout = new BufferedOutputStream(out, BUFFER_SIZE); 
         try { 
          while ((size = zin.read(buffer, 0, BUFFER_SIZE)) != -1) { 
           fout.write(buffer, 0, size); 
          } 

          zin.closeEntry(); 
         }catch (Exception e) { 
          Log.e("Exception", "Unzip exception 1:" + e.toString()); 
         } 
         finally { 
          fout.flush(); 
          fout.close(); 
         } 
        } 
       } 
      }catch (Exception e) { 
       Log.e("Exception", "Unzip exception2 :" + e.toString()); 
      } 
      finally { 
       zin.close(); 
      } 
      return true; 
     } 
     catch (Exception e) { 
      Log.e("Exception", "Unzip exception :" + e.toString()); 
     } 
     return false; 

    }