2011-10-08 40 views
16

Tôi có mật khẩu được nén bảo vệ tệp video được lưu trên thẻ sd trên trình giả lập Android. Bây giờ tôi muốn giải nén tập tin video trên thẻ sd thông qua mã. Làm thế nào tôi có thể đạt được điều đó? Bất kỳ trợ giúp hoặc mã nào? Cảm ơn trướcGiải nén tệp đã nén trên thẻ sd trong ứng dụng Android

+0

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

+0

Có một cái nhìn tại ZipInputStream từ trang web nhà phát triển Android: http://developer.android.com/reference/java /util/zip/ZipFile.html – Seph

+1

Câu hỏi này đã được hỏi nhiều lần trước đây. Nó nằm trong thư viện Java chứ không phải thư viện Android. Xem tại đây: http://stackoverflow.com/questions/3382996/how-to-unzip-files-programmatically-in-android – HXCaine

Trả lời

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(); 
    } 
    } 
} 

Trong trường hợp của bạn ::

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

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

hey trang này đang hiển thị "trang không tồn tại" –

+2

Divyesh Cảm ơn bạn đã trả lời. Nhưng tôi vẫn còn nhầm lẫn vì tệp nén của tôi được bảo vệ bằng mật khẩu nên tôi sẽ khớp với mật khẩu đó để nhập vào tệp như thế nào? –

+13

Chỉ cần bổ sung cho câu trả lời của bạn, mục nhập thực tế đọc và ghi tệp có thể được thực hiện theo khối cho hiệu suất cao hơn nhiều thay vì byte theo byte: 'byte [] buffer = new byte [4096]; cho (int c = zin.read (buffer); c! = -1; c = zin.read (buffer)) { fout.write (bộ đệm, 0, c); } ' – nobre

2

Đây là nhẹ hơn phiên bản sạch mã Samir với sử dụng Apache IOUtils.copy() cho các tập tin sao chép và finally khối. Nếu bạn có tệp lớn trong kho lưu trữ thì hãy sử dụng tốt hơn 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

Để giải nén mật khẩu bảo vệ tập tin sử dụng thư viện này:

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

nó là dễ dàng như vậy.

ZipFile zipFile = new ZipFile(YourZipFile); 
if(zipFile.isEncrypted()) 
    zipFile.setPassword(Password); 
zipFile.extractAll(Destination); 
Các vấn đề liên quan