2013-03-26 46 views
7

Tôi đặt tệp zip vào nội dung android. Làm cách nào để trích xuất tệp trong bộ nhớ trong của Android? Tôi biết làm thế nào để có được tập tin, nhưng tôi không biết làm thế nào để giải nén nó. Đây là mã của tôi ..Giải nén tệp trong Tài sản Android

Util zip ;

zip = new Util();

zip.copyFileFromAsset(this, "myfile.zip", getExternalStorage()+ "/android/data/edu.binus.profile/");

Cảm ơn vì đã giúp: D

+0

gì vấn đề u đang nhận được? nơi bạn muốn giải nén tệp zip bên trong bộ nhớ trong hoặc Bộ nhớ ngoài? –

+0

Bạn biết rằng tài sản đang được công cụ Android nén, dù sao đi nữa. Vì vậy, bạn đang thực hiện nén kép bằng cách đặt một tệp zip vào đó. Nếu bạn muốn gộp các tệp lại với nhau, bạn có thể: sử dụng tar? làm nén của riêng bạn mà không nén? Nếu gói không phải là mục tiêu, chỉ cần đặt các tập tin thô và để nén cho chuỗi công cụ Android. – akauppi

Trả lời

14

Đoạn mã này sẽ giúp bạn .... Chỉ cần chuyển vị trí tệp nén và vị trí mà bạn muốn tệp được trích xuất được lưu vào lớp này trong khi tạo đối tượng ... và gọi phương thức giải nén ...

public class Decompress { 
    private String zip; 
    private String loc; 

    public Decompress(String zipFile, String location) { 
    zip = zipFile; 
    loc = location; 

    dirChecker(""); 
    } 

    public void unzip() { 
    try { 
     FileInputStream fin = new FileInputStream(zip); 
     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(loc + 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(); 
    } 
    } 
} 
+0

Cảm ơn. Nó hoạt động: D – kyuu

+0

Vui lòng giúp bạn nếu nó hoạt động hỗ trợ tôi bằng cách upvoting .... –

+0

hi i đã thử giải nén một tệp 20mb và mất nhiều thời gian .. –

1

Có lẽ bạn nên thử sử dụng một FileOutputStream kết hợp với một InputStream từ tệp zip. Với một tệp gói, thao tác này sẽ hoạt động.

Để báo @wordy từ this question:

PackageManager pm = context.getPackageManager(); 
String apkFile = pm.getApplicationInfo(context.getPackageName(), 0).sourceDir; 
ZipFile zipFile = new ZipFile(apkFile); 
ZipEntry entry = zipFile.getEntry("assets/FILENAME"); 
myInput = zipFile.getInputStream(entry); 
myOutput = new FileOutputStream(file); 
    byte[] buffer = new byte[1024*4]; 
int length; 
int total = 0; 
int counter = 1; 
while ((length = myInput.read(buffer)) > 0) { 
    total += length; 
    counter++; 
    if (counter % 32 == 0) { 
     publishProgress(total); 
    } 
     myOutput.write(buffer, 0, length); 
} 

Hình như có thể có vấn đề với ProGuard nhưng hy vọng những mẫu mã phù hợp với bạn.

0

Tôi chưa thử nghiệm, nhưng khi thực hiện dự án trên OCR Tôi đã xem qua số library này, nơi có phương thức giải nén tệp đã tải xuống từ mạng. Phương pháp chính xác để giải nén tập tin là installZipFromAssets(String sourceFilename,File destinationDir,File destinationFile) được tìm thấy trong số này class. Hy vọng đây là những gì bạn đang tìm kiếm

0

Bạn cũng có thể sử dụng thư viện bên ngoài zip4j cung cấp các tính năng bổ sung như mã hóa. Ngoài ra, nó có chức năng để trích xuất các tập tin đến một vị trí cụ thể cung cấp đường dẫn.

5

Dựa trên giải pháp Sreedev R, tôi đã thêm các tùy chọn để đọc các tập tin từ tài sản và sử dụng đệm:

package com.pixoneye.api.utils; 

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

import android.content.Context; 
import android.util.Log; 

public class Decompress { 
    private static final int BUFFER_SIZE = 1024 * 10; 
    private static final String TAG = "Decompress"; 

    public static void unzipFromAssets(Context context, String zipFile, String destination) { 
     try { 
      if (destination == null || destination.length() == 0) 
       destination = context.getFilesDir().getAbsolutePath(); 
      InputStream stream = context.getAssets().open(zipFile); 
      unzip(stream, destination); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 

    public static void unzip(String zipFile, String location) { 
     try { 
      FileInputStream fin = new FileInputStream(zipFile); 
      unzip(fin, location); 
     } catch (FileNotFoundException e) { 
      e.printStackTrace(); 
     } 

    } 

    public static void unzip(InputStream stream, String destination) { 
     dirChecker(destination, ""); 
     byte[] buffer = new byte[BUFFER_SIZE]; 
     try { 
      ZipInputStream zin = new ZipInputStream(stream); 
      ZipEntry ze = null; 

      while ((ze = zin.getNextEntry()) != null) { 
       Log.v(TAG, "Unzipping " + ze.getName()); 

       if (ze.isDirectory()) { 
        dirChecker(destination, ze.getName()); 
       } else { 
        File f = new File(destination + ze.getName()); 
        if (!f.exists()) { 
         FileOutputStream fout = new FileOutputStream(destination + ze.getName()); 
         int count; 
         while ((count = zin.read(buffer)) != -1) { 
          fout.write(buffer, 0, count); 
         } 
         zin.closeEntry(); 
         fout.close(); 
        } 
       } 

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

    } 

    private static void dirChecker(String destination, String dir) { 
     File f = new File(destination + dir); 

     if (!f.isDirectory()) { 
      boolean success = f.mkdirs(); 
      if (!success) { 
       Log.w(TAG, "Failed to create folder " + f.getName()); 
      } 
     } 
    } 
} 
Các vấn đề liên quan