Android-无法从SD卡解压缩文件

时间:2013-03-08 00:43:56

标签: java android zip unzip

正如标题所述,我正在尝试解压缩已在SD卡上复制的文件。当我尝试时,它会创建目录,但不会提取。我将在一秒钟内提供我的代码。我还想问一下如何通过进度条来显示提取进度。无论如何,这是代码:

package com.cydeon.smirkit;

import java.io.BufferedInputStream;
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.concurrent.TimeoutException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

import com.stericson.RootTools.*;
import com.stericson.RootTools.exceptions.RootDeniedException;
import com.stericson.RootTools.execution.CommandCapture;

import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.res.AssetManager;

public class MainActivity extends Activity implements OnClickListener {


public void copyAssest(){
    File directory = new File("/sdcard/smirkit.zip");
    if(directory.exists() && directory.isDirectory()){
        //Do Nothing
    }else{
        AssetManager assetManager = getAssets();
        String[] files = null;
        try {
            files = assetManager.list("");

        }catch (IOException e) {
            Log.e("tag", "Failed to get asset file list.", e);
        }
        for(String filename : files) {
            InputStream in = null;
            OutputStream out = null;
            try {
                in = assetManager.open(filename);
                out = new FileOutputStream("/sdcard/" + filename);
                copyFile(in, out);
                in.close();
                in = null;
                out.flush();
                out.close();
                out = null;

                } catch(IOException e) {
                    Log.e("tag", "Failed to copy asset file: " + filename, e);

                }
        }}

    }

    private void copyFile(InputStream in, OutputStream out) throws IOException {
        byte[] buffer = new byte[1024];
        int read;
        while((read = in.read(buffer)) != -1){
            out.write(buffer, 0, read);
        }
    }


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    copyAssest();
    if (RootTools.isBusyboxAvailable()){
    } else {
        RootTools.offerBusyBox(MainActivity.this);
    }
    Button bInstall = (Button) findViewById(R.id.bInstall);
    bInstall.setOnClickListener(this);
    Button bUninstall = (Button) findViewById(R.id.bRemove);
    bUninstall.setOnClickListener(this);


}

@Override
public void onClick(View arg0) {
    switch(arg0.getId()){
        case R.id.bInstall:
                String path = "/sdcard/";
                String zipname = "smirkit.zip";
                InputStream is;
                ZipInputStream zis;
                try{
                    String filename;
                    is = new FileInputStream(path + zipname);
                    zis = new ZipInputStream(new BufferedInputStream(is));
                    ZipEntry ze;
                    byte[] buffer = new byte[1024];
                    int count;

                    while((ze = zis.getNextEntry()) != null){
                        filename = ze.getName();

                        if (ze.isDirectory()){
                            File fmd = new File(path + filename);
                            fmd.mkdirs();
                            continue;
                            }
                    FileOutputStream fout = new FileOutputStream(path = filename);

                    while((count = zis.read(buffer)) != -1){
                        fout.write(buffer, 0, count);
                    }

                    }
                } catch (FileNotFoundException e) {
                    Log.e(zipname, zipname);
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            break;

}
}}

0 个答案:

没有答案