마스터Q&A 안드로이드는 안드로이드 개발자들의 질문과 답변을 위한 지식 커뮤니티 사이트입니다. 안드로이드펍에서 운영하고 있습니다. [사용법, 운영진]

안드로이드 내에있는 zip파일 풀기

0 추천

일단 코드는...

import android.os.Environment;
import android.util.Log;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

public class Decompress {
    private String _location; //압출을 풀 위치

    String unzipLocation = Environment.getExternalStorageDirectory() + "/unzipped/"//unzip 하고자 하는 위치

    Decompress d = new Decompress(unzipLocation);
    d.unzip();

    public Decompress(String location) {
        _location = location;

        _dirChecker(""); //폴더를 만들기 위한 함수로 아래에 정의 되어 있습니다.
    }

    public void unzip() {
        try  {
            final ZipInputStream zin = new ZipInputStream(getResources().openRawResource(R.drawable.asd));

//이 부분을 바꿔어 주셔야 합니다. 저의 경우 zip 파일이 res/raw 폴더 안의 haa.zip 이었습니다.

            ZipEntry ze = null;
            while ((ze = zin.getNextEntry()) != null) {
                Log.v("Decompress", "Unzipping " + ze.getName());   //압축이 풀리면서 logcat으로 zip 안에 있던 파일        if(ze.isDirectory()) {                                  들을 볼 수 있습니다.
                if(ze.isDirectory()) {
                _dirChecker(ze.getName());
            } else {
                FileOutputStream fout = new FileOutputStream(_location + ze.getName());
                BufferedInputStream in = new BufferedInputStream(zin);
                BufferedOutputStream out = new BufferedOutputStream(fout);
                byte b[] = new byte[1024];
                int n;
                while ((n = in.read(b,0,1024)) >= 0) {
                    out.write(b,0,n);
                }

                zin.closeEntry();
                fout.close();
            }

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

}

    //변수 location에 저장된 directory의 폴더를 만듭니다.
    private void _dirChecker(String dir) {
        File f = new File(_location + dir);

        if(!f.isDirectory()) {
            f.mkdirs();
        }
    }

    }

이렇게 되어있습니다

[소스 출처http://202psj.tistory.com/678]

Decompress d = new Decompress(unzipLocation);
    d.unzip();

이부분의 unzip(); 이 오류납니다..그리고

public void unzip() {
        try  {
            final ZipInputStream zin = new ZipInputStream(getResources().openRawResource(R.drawable.asd));

 

이부분의 getResources()가 오류가 납니다..

[혹시몰라서]이것들은 Decompress.java 안에 있는 내용입니다

제발도와주세요 ㅜ

익명사용자 님이 2016년 6월 12일 질문

1개의 답변

0 추천
getResources 앞에 context 정보가 필요해 보입니다.

https://developer.android.com/reference/android/content/Context.html#getResources()
aucd29 (218,390 포인트) 님이 2016년 6월 13일 답변
...