2011-10-08 136 views
16

我有一个压缩密码保护在android模拟器上的SD卡上保存的视频文件。现在我想通过代码解压缩SD卡上的视频文件。我怎么能做到这一点?任何帮助或代码? 在此先感谢解压在Android应用程序的SD卡上的压缩文件

+0

http://www.google.com/search?q=android+unzip+file – Caner

+0

从Android开发者网站看看ZipInputStream:http://developer.android.com/reference/java /util/zip/ZipFile.html – Seph

+1

此问题之前已被询问过很多次。它位于Java库中,而不是Android。看到这里:http://stackoverflow.com/questions/3382996/how-to-unzip-files-programmatically-in-android – HXCaine

回答

21
import android.util.Log; 
import java.io.File; 
import java.io.FileInputStream; 
import java.io.FileOutputStream; 
import java.util.zip.ZipEntry; 
import java.util.zip.ZipInputStream; 

/** 
* 
* @author jon 
*/ 
public class Decompress { 
    private String _zipFile; 
    private String _location; 

    public Decompress(String zipFile, String location) { 
    _zipFile = zipFile; 
    _location = location; 

    _dirChecker(""); 
    } 

    public void unzip() { 
    try { 
     FileInputStream fin = new FileInputStream(_zipFile); 
     ZipInputStream zin = new ZipInputStream(fin); 
     ZipEntry ze = null; 
     while ((ze = zin.getNextEntry()) != null) { 
     Log.v("Decompress", "Unzipping " + ze.getName()); 

     if(ze.isDirectory()) { 
      _dirChecker(ze.getName()); 
     } else { 
      FileOutputStream fout = new FileOutputStream(_location + ze.getName()); 
      for (int c = zin.read(); c != -1; c = zin.read()) { 
      fout.write(c); 
      } 

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

     } 
     zin.close(); 
    } catch(Exception e) { 
     Log.e("Decompress", "unzip", e); 
    } 

    } 

    private void _dirChecker(String dir) { 
    File f = new File(_location + dir); 

    if(!f.isDirectory()) { 
     f.mkdirs(); 
    } 
    } 
} 

在你的情况::

String zipFilename = Environment.getExternalStorageDirectory() + "/files.zip"; 
String unzipLocation = Environment.getExternalStorageDirectory() + "/unzipped/"; 

Decompress d = new Decompress(zipFilename, unzipLocation); 
d.unzip(); 
+0

嘿这个网页显示“该页面不存在” –

+2

Divyesh感谢您的答复。但我仍然困惑,因为我的压缩文件是密码保护,所以我将如何匹配该密码进入文件? –

+13

只需对你的答案进行补充,实际的入口读取和文件写入就可以以更高的性能完成,而不是逐字节地执行:'byte [] buffer = new byte [4096]; for(int c = zin.read(buffer); c!= -1; c = zin.read(buffer)){fout.write(buffer,0,c); }' – nobre

2

这稍微更是使用Apache的IOUtils.copy()用于复制文件和finally块清洁版本的萨米尔的代码。如果档案中有大文件,最好使用IOUtils.copyLarge()

import org.apache.commons.io.IOUtils; 

import java.io.File; 
import java.io.FileOutputStream; 
import java.io.IOException; 
import java.io.InputStream; 
import java.util.zip.ZipEntry; 
import java.util.zip.ZipInputStream; 

public class ZipUtils { 
    public static void unzip(InputStream is, File path) { 
     checkDir(path); 
     ZipInputStream zis = null; 
     FileOutputStream fos = null; 
     try { 
      zis = new ZipInputStream(is); 
      ZipEntry ze; 
      while ((ze = zis.getNextEntry()) != null) { 
       File entryFile = new File(path, ze.getName()); 
       if (ze.isDirectory()) { 
        checkDir(entryFile); 
       } else { 
        fos = new FileOutputStream(entryFile); 
        IOUtils.copy(zis, fos); 
        fos.close(); 
        fos = null; 
       } 
       zis.closeEntry(); 
      } 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } finally { 
      if (zis != null) { 
       try { 
        zis.close(); 
       } catch (IOException ignore) { 
       } 
      } 
      if (fos != null) { 
       try { 
        fos.close(); 
       } catch (IOException ignore) { 
       } 
      } 
     } 
    } 

    private static void checkDir(File path) { 
     if (!path.exists()) { 
      path.mkdirs(); 
     } else if (!path.isDirectory()) { 
      throw new IllegalArgumentException("Path is not directory"); 
     } 
    } 
} 
5

拆包密码保护的文件,请使用这个库:

http://www.lingala.net/zip4j/download.php

它是那么容易。

ZipFile zipFile = new ZipFile(YourZipFile); 
if(zipFile.isEncrypted()) 
    zipFile.setPassword(Password); 
zipFile.extractAll(Destination);