开发者

Struggling with Unzipping A File

开发者 https://www.devze.com 2023-04-10 13:02 出处:网络
Here is my code: String mixtapefilename = \"testzip.zip\"; String zipname = mixtapefilename; String path = Environment.getExternalStorageDirectory() + \"/download/\";

Here is my code:

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 File开发者_Go百科InputStream(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;
}

And my error is: /mnt/sdcard/download/testzip/testzip.mp3 (No such file or directory)

So it can't find my MP3? But it is supposed to be unzipping my mp3, do I somehow need to create the directories first?


Yes, you have to create necessary files and directory first before you do unzipping. I believe your sd card already have download directory in it. If so, try this code below. Feel free to adjust to your need.

    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;
}
0

精彩评论

暂无评论...
验证码 换一张
取 消