2011-10-08 20 views
16

Android emülatöründe sd kartına kaydedilmiş bir video dosyası korumalı bir sıkıştırılmış parolam var. Şimdi bu video dosyasını sd kartında kod aracılığıyla açmak istiyorum. Bunu nasıl başarabilirim? Herhangi bir yardım ya da kod? Senin durumunda peşinAndroid uygulamasında sd kartında sıkıştırılmış bir dosyayı açın

+0

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

+0

Android geliştirici sitesinden ZipInputStream göz at: http://developer.android.com/reference/java /util/zip/ZipFile.html – Seph

+1

Bu soruya daha önce defalarca sorulmuştur. Android'den ziyade Java kütüphanesinde. Buraya bakın: http://stackoverflow.com/questions/3382996/how-to-unzip-files-programmatically-in-android – HXCaine

cevap

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

yılında Teşekkür ::

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

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

hey bu sayfa "sayfa mevcut değil" –

+2

Divyesh Yanıt için teşekkürler. Ama yine de kafam karıştı çünkü sıkıştırılmış dosyam parola korumalı, böylece dosyaya girmek için bu parolayı nasıl eşleştireceğim? –

+13

Cevabınız için sadece bir tamamlayıcı, gerçek giriş okuma ve dosya yazma bayt yerine bayt yerine daha yüksek performans için parçalar halinde yapılabilir: 'byte [] buffer = new byte [4096]; (int c = zin.read (buffer); c! = -1; c = zin.read (buffer)) için { fout.write (tampon, 0, c);} ' – nobre

2

Bu biraz daha Apache en IOUtils.copy() kopyalama dosyalar için ve finally blok kullanarak Samir kodunun temizleyici versiyonudur. Arşivde büyük dosyalarınız varsa IOUtils.copyLarge()'u daha iyi kullanın. Şifre korumalı dosya kullanacağım bu kütüphaneyi açma için

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"); 
     } 
    } 
} 
İlgili konular