解压缩文件挣扎

时间:2011-10-05 03:21:45

标签: android

这是我的代码:

String mixtapefilename = "testzip.zip";
String zipname = mixtapefilename; 
        String path = Environment.getExternalStorageDirectory() + "/download/";              
        unpackZip(path, zipname);

private boolean unpackZip(String path, String zipname)
{       
     InputStream is;
     ZipInputStream zis;
     try 
     {
         is = new FileInputStream(path + zipname);
         zis = new ZipInputStream(new BufferedInputStream(is));          
         ZipEntry ze;

         while ((ze = zis.getNextEntry()) != null) 
         {
             ByteArrayOutputStream baos = new ByteArrayOutputStream();
             byte[] buffer = new byte[1024];
             int count;

             // zapis do souboru
             String filename = ze.getName();
             FileOutputStream fout = new FileOutputStream(path + filename);

             // cteni zipu a zapis
             while ((count = zis.read(buffer)) != -1) 
             {
                 baos.write(buffer, 0, count);
                 byte[] bytes = baos.toByteArray();
                 fout.write(bytes);             
                 baos.reset();
             }

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

         zis.close();
         Toast toast = Toast.makeText(getApplicationContext(),"Download Complete", Toast.LENGTH_LONG);
         toast.show();
     } 
     catch(IOException e)
     {
         ProgressDialog dialog;
         dialog = new ProgressDialog(Download.this);                
         dialog.setMessage(e.toString());
         dialog.show();

         return false;
     }

    return true;
}

我的错误是:/mnt/sdcard/download/testzip/testzip.mp3(没有这样的文件或目录)

所以它找不到我的MP3?但它应该是解压缩我的mp3,我是否需要先创建目录?

1 个答案:

答案 0 :(得分:0)

是的,在解压缩之前,您必须先创建必要的文件和目录。我相信你的SD卡已经有了下载目录。如果是这样,请尝试以下代码。随意调整您的需求。

    private boolean unpackZip(String path, String zipname){       
     InputStream is;
     ZipInputStream zis;
     try 
     {
         is = new FileInputStream(path + zipname);
         zis = new ZipInputStream(new BufferedInputStream(is));          
         ZipEntry ze;

         while ((ze = zis.getNextEntry()) != null) 
         {
             ByteArrayOutputStream baos = new ByteArrayOutputStream();
             byte[] buffer = new byte[1024];
             int count;

             // zapis do souboru
             String filename = ze.getName();
     File innerFile = new File(path, fileName);

             if (ze.isDirectory()) {
        Log.d("DEBUG", "The Entry is a directory..");
        innerFile.mkdirs();
     } else {
             FileOutputStream fout = new FileOutputStream(path + filename);

             // cteni zipu a zapis
             while ((count = zis.read(buffer)) != -1) 
             {
                 baos.write(buffer, 0, count);
                 byte[] bytes = baos.toByteArray();
                 fout.write(bytes);             
                 baos.reset();
             }

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

         zis.close();
         Toast toast = Toast.makeText(getApplicationContext(),"Download Complete", Toast.LENGTH_LONG);
         toast.show();
       }
     } 
     catch(IOException e)
     {
         ProgressDialog dialog;
         dialog = new ProgressDialog(Download.this);                
         dialog.setMessage(e.toString());
         dialog.show();

         return false;
     }

    return true;
}