1

我正在使用以下代码解压缩文件。

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    String zipFilePath = Environment.getExternalStorageDirectory()
            .getAbsolutePath();
    unpackZip(zipFilePath,"/MyRar.zip");
}
private boolean unpackZip(String path, String zipname)
{       
     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) 
         {
             // zapis do souboru
             filename = ze.getName();

             // Need to create directories if not exists, or
             // it will generate an Exception...
             if (ze.isDirectory()) {
                File fmd = new File(path + filename);
                fmd.mkdirs();
                continue;
             }

             FileOutputStream fout = new FileOutputStream(path + filename);

             // cteni zipu a zapis
             while ((count = zis.read(buffer)) != -1) 
             {
                 fout.write(buffer, 0, count);             
             }

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

         zis.close();
     } 
     catch(IOException e)
     {
         e.printStackTrace();
         return false;
     }

    return true;
}
}

我也在<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />清单文件中添加了。

但它显示文件未找到异常。

日志猫:

java.io.FileNotFoundException: /mnt/sdcardMyRar/list.txt: open failed: ENOENT (No such file or directory)
at libcore.io.IoBridge.open(IoBridge.java:406)
at java.io.FileOutputStream.<init>(FileOutputStream.java:88)
at java.io.FileOutputStream.<init>(FileOutputStream.java:128)
at java.io.FileOutputStream.<init>(FileOutputStream.java:117)
at com.example.unzipfile.MainActivity.unpackZip(MainActivity.java:66)
at com.example.unzipfile.MainActivity.onCreate(MainActivity.java:26)
at android.app.Activity.performCreate(Activity.java:4492)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
4

1 回答 1

1

现在您的目录是:/mnt/sdcardMyRar/检查目录,它不正确!

检查以下建议:

String zipFilePath = Environment.getExternalStorageDirectory()
                       .getAbsolutePath() + "/";
于 2013-09-20T10:03:01.430 回答