在Android应用程序中解压缩sd卡上的压缩文件

Suh*_*rik 15 android file unzip sd-card

我有一个压缩密码保护在Android模拟器上的SD卡上保存的视频文件.现在我想通过代码在SD卡上解压缩该视频文件.我怎样才能实现这一目标?任何帮助或代码?提前致谢

Sam*_*iya 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(); 
    } 
  } 
} 
Run Code Online (Sandbox Code Playgroud)

在你的情况下::

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

Decompress d = new Decompress(zipFilename, unzipLocation); 
d.unzip(); 
Run Code Online (Sandbox Code Playgroud)

  • 只是对你的答案的补充,实际的条目读取和文件写入可以在块中完成,以获得更高的性能而不是逐字节:`byte [] buffer = new byte [4096]; for(int c = zin.read(buffer); c!= -1; c = zin.read(buffer)){fout.write(buffer,0,c); }` (13认同)
  • Divyesh感谢您的回复.但我仍然感到困惑,因为我的压缩文件受密码保护,所以我将如何匹配该密码进入文件? (2认同)

Beh*_*z.M 5

要解压缩受密码保护的文件,请使用此库:

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

它很容易.

ZipFile zipFile = new ZipFile(YourZipFile);
if(zipFile.isEncrypted())
    zipFile.setPassword(Password);
zipFile.extractAll(Destination);
Run Code Online (Sandbox Code Playgroud)


归档时间:

查看次数:

24209 次

最近记录:

7 年,3 月 前