开发者

FileNotFoundException while unzipping a file

I have this code that's supposed to unzipp a file.

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()) 开发者_JAVA百科{ 
                f.mkdirs(); 
              } 

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

            finish();

    }

}

Yet it only throws "ava.io.FileNotFoundException: /mnt/sdcard/unzipped/Eng_blue/altlayout.txt" which is the name of the first file and dies.

I thought that dirChecker() method would create this folders on the flow... any ideas how to fix that?

Thanks!


Make sure you added the <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> permission to your AndroidManifest.xml file


Use this:

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);
        }
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜