Android 解压缩文件时FileNotFoundException

Android 解压缩文件时FileNotFoundException,android,Android,我有一个应该解压文件的代码 public class dec extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Toast.makeText(this, "hello, starting to unZipp!", 15500).show();

我有一个应该解压文件的代码

public class dec extends Activity {

    @Override
        public void onCreate(Bundle savedInstanceState) {

            super.onCreate(savedInstanceState);

            Toast.makeText(this, "hello, starting to unZipp!", 15500).show();

            String location = Environment.getExternalStorageDirectory() + "/unzipped/"; 
            /////////////////////////////////////////////////////////////////////

            try  { 

                ZipInputStream zin = new ZipInputStream(getAssets().open("totalkeys.zip")); 

                ZipEntry ze = null; 

                while ((ze = zin.getNextEntry()) != null) { 
                  Log.v("Decompress", "My UNZIPPING: " + ze.getName()); 
                  if(ze.isDirectory()) { 
                    dirChecker(ze.getName()); 
                  } else { 
                    FileOutputStream fout = new FileOutputStream(location + 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.v("Decompress", "My ERROR: "+e.toString()); 
                /// throws My ERROR: java.io.FileNotFoundException: /mnt/sdcard/unzipped/Eng_blue/altlayout.txt (No such file or directory)
                /// and dies.
              } 
            } 

            ////////////////////////////////////////////////////

            private void dirChecker(String dir) { 

              String location = Environment.getExternalStorageDirectory() + "/unzipped/"; 
              File f = new File(location + dir); 

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

            ////////////////////////////////////////////////////

            finish();

    }

}
但它只抛出“ava.io.FileNotFoundException:/mnt/sdcard/unzip/Eng_blue/altlayout.txt”,这是第一个文件的名称,然后消失

我以为dirChecker()方法会在流上创建此文件夹。。。有没有办法解决这个问题


谢谢

确保已将
权限添加到AndroidManifest.xml文件中

使用以下命令:

public class Unzipper extends Observable {

private static final String TAG = "UnZip";
private String mFileName, mFilePath, mDestinationPath;

public Unzipper (String fileName, String filePath, String destinationPath) {
    mFileName = fileName="test";
    mFilePath = filePath="sdcard/folder";
    mDestinationPath = destinationPath="/sdcard/folder1/";
}

public String getFileName () {
    return mFileName;
}

public String getFilePath() {
    return mFilePath;
}

public String getDestinationPath () {
    return mDestinationPath;
}

public void unzip () {
    String fullPath = mFilePath + "/" + mFileName + ".zip";
    Log.d(TAG, "unzipping " + mFileName + " to " + mDestinationPath);
    new UnZipTask().execute(fullPath, mDestinationPath);
}

private class UnZipTask extends AsyncTask<String, Void, Boolean> {

    @SuppressWarnings("rawtypes")
    @Override
    protected Boolean doInBackground(String... params) {
        String filePath = params[0];
        String destinationPath = params[1];

        File archive = new File(filePath);
        try {
            ZipFile zipfile = new ZipFile(archive);
            for (Enumeration e = zipfile.entries(); e.hasMoreElements();) {
                ZipEntry entry = (ZipEntry) e.nextElement();
                unzipEntry(zipfile, entry, destinationPath);
            }
        } catch (Exception e) {
            Log.e(TAG, "Error while extracting file " + archive, e);
            return false;
        }

        return true;
    }

    @Override
    protected void onPostExecute(Boolean result) {
        setChanged();
        notifyObservers();
    }

    private void unzipEntry(ZipFile zipfile, ZipEntry entry,
            String outputDir) throws IOException {

        if (entry.isDirectory()) {
            createDir(new File(outputDir, entry.getName()));
            return;
        }

        File outputFile = new File(outputDir, entry.getName());
        if (!outputFile.getParentFile().exists()) {
            createDir(outputFile.getParentFile());
        }

        Log.v(TAG, "Extracting: " + entry);
        BufferedInputStream inputStream = new BufferedInputStream(zipfile.getInputStream(entry));
        BufferedOutputStream outputStream = new BufferedOutputStream(new FileOutputStream(outputFile));

        try {

        } finally {
            outputStream.close();
            inputStream.close();
        }
    }

    private void createDir(File dir) {
        if (dir.exists()) {
            return;
        }
        Log.v(TAG, "Creating dir " + dir.getName());
        if (!dir.mkdirs()) {
            throw new RuntimeException("Can not create dir " + dir);
        }
    }
}
public类不可见{
私有静态最终字符串TAG=“UnZip”;
私有字符串mFileName、mFilePath、mDestinationPath;
公共解压(字符串文件名、字符串文件路径、字符串目标路径){
mFileName=fileName=“测试”;
mFilePath=filePath=“SD卡/文件夹”;
mdestationpath=destinationPath=“/sdcard/folder1/”;
}
公共字符串getFileName(){
返回mFileName;
}
公共字符串getFilePath(){
返回mFilePath;
}
公共字符串getDestinationPath(){
返回mdestationpath;
}
公共void解压(){
字符串fullPath=mFilePath+“/”+mFileName+“.zip”;
Log.d(标记,“解压缩”+mFileName+“到”+MDestationPath);
新建UnZipTask().execute(完整路径、mDestinationPath);
}
私有类UnZipTask扩展异步任务{
@抑制警告(“原始类型”)
@凌驾
受保护的布尔doInBackground(字符串…参数){
字符串filePath=params[0];
字符串destinationPath=params[1];
文件存档=新文件(文件路径);
试一试{
ZipFile ZipFile=新ZipFile(存档);
对于(枚举e=zipfile.entries();e.hasMoreElements();){
ZipEntry条目=(ZipEntry)e.nextElement();
取消ZipPentry(zipfile、entry、destinationPath);
}
}捕获(例外e){
Log.e(标记“提取文件时出错”+存档,e);
返回false;
}
返回true;
}
@凌驾
受保护的void onPostExecute(布尔结果){
setChanged();
通知观察员();
}
私密无效(ZipFile ZipFile、ZipPentry入口、,
字符串outputDir)引发IOException{
if(entry.isDirectory()){
createDir(新文件(outputDir,entry.getName());
返回;
}
File outputFile=新文件(outputDir,entry.getName());
如果(!outputFile.getParentFile().exists()){
createDir(outputFile.getParentFile());
}
Log.v(标签,“提取:”+条目);
BufferedInputStream inputStream=新的BufferedInputStream(zipfile.getInputStream(条目));
BufferedOutputStream outputStream=新的BufferedOutputStream(新文件outputStream(outputFile));
试一试{
}最后{
outputStream.close();
inputStream.close();
}
}
私有void createDir(文件目录){
if(dir.exists()){
返回;
}
Log.v(标记“Creating dir”+dir.getName());
如果(!dir.mkdirs()){
抛出新的运行时异常(“无法创建目录”+dir);
}
}
}