9

androidアセットにzipファイルを入れました。Androidの内部ストレージにファイルを抽出するにはどうすればよいですか?ファイルの取得方法は知っていますが、抽出方法がわかりません。これは私のコードです。

Util zip ;

zip = new Util();

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

助けてくれてありがとう:D

4

5 に答える 5

15

このコードはあなたを助けます....オブジェクトを作成している間、zipfileの場所と抽出されたファイルをこのクラスに保存したい場所を渡すだけです...そしてunzipメソッドを呼び出します...

    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(); 
    } 
  } 
} 
于 2013-03-26T05:09:43.680 に答える
13

Sreedev Rソリューションに基づいて、アセットからファイルを読み取り、バッファーを使用するオプションを追加しました。

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()) {
                        boolean success = f.createNewFile();
                        if (!success) {
                            Log.w(TAG, "Failed to create file " + f.getName());
                            continue;
                        }
                        FileOutputStream fout = new FileOutputStream(f);
                        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());
            }
        }
    }
}
于 2014-12-09T09:36:30.387 に答える
1

たぶん、zipファイルからのinputstreamと組み合わせてFileOutputStreamを使用してみてください。パッケージファイルを使用すると、これは機能するはずです。

この質問から@wordyを引用するには:

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

ProGuardに問題があるようですが、コードサンプルが機能することを願っています。

于 2013-03-26T02:54:22.757 に答える
0

まだテストしていませんが、プロジェクトを行っているときに、ダウンロードしたファイルをネットから解凍する方法があるこのライブラリOCRに出くわしました。ファイルを解凍するための正確なメソッドは、このクラスの下にあります。これがあなたが探しているものであることを願っていますinstallZipFromAssets(String sourceFilename,File destinationDir,File destinationFile)

于 2013-03-26T03:03:41.790 に答える
0

暗号化などの追加機能を提供するzip4j外部ライブラリを利用することもできます。また、パスを指定して特定の場所にファイルを抽出する機能もあります。

于 2013-03-26T04:25:09.440 に答える