Android

unzip

aucd29 2013. 10. 8. 14:39
link : http://hsnote.tistory.com/entry/Android-Unzip-%EA%B8%B0%EB%8A%A5

package com.infraware.ziptest;

import java.io.DataInputStream;
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.io.OutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;


import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.webkit.WebView;
import android.widget.TextView;
import android.content.Context;
import java.util.*;

public class ZipTestAppActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);        
        
        m_strZipFileName = "girlfriend.zip";
        
        File dir = getFilesDir();
        
        try {
        
            File unzipFile = new File(dir,m_strZipFileName);
            unZip(unzipFile);
            
            int nPoint = m_strZipFileName.indexOf(".zip");
            String strStartFile = dir.getAbsolutePath() + File.separator + m_strZipFileName.substring(0, nPoint) + File.separator + "index.html";
            Log.d("infraware",strStartFile);
            WebView runtimeView = new WebView(this);
            runtimeView.loadUrl("http://m.naver.com");
            //runtimeView.loadUrl("file:///index.html");
            //runtimeView.loadData("<a href='index.html'>girlfriend</a>", "text/html", "utf-8");
            
            setContentView(runtimeView);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            TextView errMsgView = new TextView(this);
            errMsgView.setText(e.toString());
            setContentView(errMsgView);
        }    
        
        
    }
    
    private void unZip(File unZipFile) throws IOException{        
        ZipInputStream in = new ZipInputStream(new FileInputStream(unZipFile));        
        ZipEntry ze;
         
        File dir = getFilesDir();
        int nPoint = m_strZipFileName.indexOf(".zip");
        Log.d("infraware",m_strZipFileName.substring(0,nPoint));
        
        String strWillUnzipFolderName = dir.getAbsolutePath() + File.separator + m_strZipFileName.substring(0,nPoint);
        
        File dirUnzipFolder = new File(strWillUnzipFolderName);
        
        if(!dirUnzipFolder.exists())
        {
            while( (ze = in.getNextEntry()) != null){
                final String path = dir.getAbsolutePath() + File.separator + m_strZipFileName.substring(0,nPoint) + File.separator + ze.getName();
                
                if(ze.getName().indexOf("/") != -1)
                {
                    File parent = new File(path).getParentFile();
                    if(!parent.exists())
                    {
                        if(!parent.mkdirs())
                            throw new IOException("Unable to create folder" + parent);
                    }
                }            
                
                FileOutputStream out = new FileOutputStream(path);
                
                byte[] buf = new byte[1024];
                
                for(int nReadSize = in.read(buf); nReadSize != -1 ; nReadSize = in.read(buf)){
                    out.write(buf, 0, nReadSize);
                }
                out.close();
            }        
            in.close();    
        }                    
    }
    
    private String m_strZipFileName;
}