如何在Android的编程方式解压文件?方式、文件、如何在、Android

由网友(所冇深愛蔸諟秘密)分享简介:我需要一个小的code段其解压缩从给定的.zip文件的几个文件,并给出了单独的文件,根据他们在压缩文件的格式。请发表你的知识和帮助我。 I need a small code snippet which unzips a few files from a given .zip file and gives the s...

我需要一个小的code段其解压缩从给定的.zip文件的几个文件,并给出了单独的文件,根据他们在压缩文件的格式。请发表你的知识和帮助我。

I need a small code snippet which unzips a few files from a given .zip file and gives the separate files according to the format they were in the zipped file. Please post your knowledge and help me out.

推荐答案

假如peno的版本优化了一下。在性能上的增加是可感知的。

Had peno's version optimised a bit. The increase in performance is perceptible.

private boolean unpackZip(String path, String zipname)
{       
     InputStream is;
     ZipInputStream zis;
     try 
     {
         String filename;
         is = new FileInputStream(path + zipname);
         zis = new ZipInputStream(new BufferedInputStream(is));          
         ZipEntry ze;
         byte[] buffer = new byte[1024];
         int count;

         while ((ze = zis.getNextEntry()) != null) 
         {
             // zapis do souboru
             filename = ze.getName();

             // Need to create directories if not exists, or
             // it will generate an Exception...
             if (ze.isDirectory()) {
                File fmd = new File(path + filename);
                fmd.mkdirs();
                continue;
             }

             FileOutputStream fout = new FileOutputStream(path + filename);

             // cteni zipu a zapis
             while ((count = zis.read(buffer)) != -1) 
             {
                 fout.write(buffer, 0, count);             
             }

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

         zis.close();
     } 
     catch(IOException e)
     {
         e.printStackTrace();
         return false;
     }

    return true;
}
阅读全文

相关推荐

最新文章