首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何在Android中以编程方式解压缩文件?

如何在Android中以编程方式解压缩文件?
EN

Stack Overflow用户
提问于 2010-08-02 01:26:04
回答 10查看 120.2K关注 0票数 140

我需要一个小的代码片段,从一个给定的.zip文件解压几个文件,并给出单独的文件,根据它们在压缩文件中的格式。请张贴你的知识,并帮助我。

EN

回答 10

Stack Overflow用户

发布于 2012-06-12 21:37:54

对peno的版本进行了一些优化。性能的提高是显而易见的。

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

             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;
}
票数 147
EN

Stack Overflow用户

发布于 2011-09-15 01:47:20

这是我的解压缩方法,我使用它:

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;

             String filename = ze.getName();
             FileOutputStream fout = new FileOutputStream(path + filename);

             // reading and writing
             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();
     } 
     catch(IOException e)
     {
         e.printStackTrace();
         return false;
     }

    return true;
}
票数 26
EN

Stack Overflow用户

发布于 2010-08-02 01:32:16

Android有内置的Java API。查看java.util.zip包。

ZipInputStream类是您应该研究的内容。从ZipInputStream中读取ZipEntry并将其转储到文件系统/文件夹中。检查similar example to compress into zip文件。

票数 13
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3382996

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档