2014-08-30 153 views
0

我尝试制作显示100张图片的应用程序。 我想压缩照片的文件夹,然后把它放在我的项目上。 现在我需要了解如何将zip文件夹复制到内部存储,然后在android中解压缩它?在Android中解压缩文件夹

回答

3

你可以将你的.zip文件包含在apk的Assets文件夹中,并将它们放在代码中,将.zip复制到内部存储并使用ZipInputStream进行解压缩。

首页复印德.zip文件的内部存储,并解压缩文件后:

protected void copyFromAssetsToInternalStorage(String filename){ 
     AssetManager assetManager = getAssets(); 

     try { 
      InputStream input = assetManager.open(filename); 
      OutputStream output = openFileOutput(filename, Context.MODE_PRIVATE); 

      copyFile(input, output); 

     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 

    private void unZipFile(String filename){ 
     try { 
      ZipInputStream zipInputStream = new ZipInputStream(openFileInput(filename)); 
      ZipEntry zipEntry; 

      while((zipEntry = zipInputStream.getNextEntry()) != null){ 
       FileOutputStream zipOutputStream = openFileOutput(zipEntry.getName(), MODE_PRIVATE); 

       int length; 
       byte[] buffer = new byte[1024]; 

       while((length = zipInputStream.read(buffer)) > 0){ 
        zipOutputStream.write(buffer, 0, length); 
       } 

       zipOutputStream.close(); 
       zipInputStream.closeEntry(); 
      } 
      zipInputStream.close(); 

     } catch (FileNotFoundException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 

    private void copyFile(InputStream in, OutputStream out) throws IOException { 
     byte[] buffer = new byte[1024]; 
     int read; 
     while((read = in.read(buffer)) != -1){ 
      out.write(buffer, 0, read); 
     } 
    } 
+0

我有这个error.what我应该做的java.lang.IllegalArgumentException异常:文件名/包含路径分隔符@ Joseph – programmer 2014-08-31 06:13:19

+0

您的资产zip文件位于Assets文件夹的根目录或另一个文件夹的内部? – Joseph 2014-09-01 16:17:09

+0

@Joseph我怎样才能解压到这个代码的特殊文件夹?例如我想提取文件到chach目录中:getApplicationContext()。getCacheDir()。toString() – mahdi 2015-07-20 11:49:23

相关问题