10:19 AM
0
This is way I implement to unzip file:
String zipFilename = Environment.getExternalStorageDirectory() + "/FileName.zip";
        String unzipLocation = Environment.getExternalStorageDirectory() + "/FileName/";
private void dirChecker(final String dir) {
        File f = new File(mLocation + dir);

        if (!f.isDirectory()) {
            f.mkdirs();
        }
    }
 Create ansyntask to unzip file:
class UnZipTask extends AsyncTask<Void, Integer, Integer> {
        /**
         * Number progress.
         */
        private int per = 0;

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            showDialog(Constants.DIALOG_UNZIP_PROGRESS);
        }

        @Override
        protected Integer doInBackground(final Void... params) {
            try {
                @SuppressWarnings("resource")
                ZipFile zipFile = new ZipFile(mZipFile);
                mProgressDialog.setMax(zipFile.size());
                FileInputStream fin = new FileInputStream(mZipFile);
                ZipInputStream zin = new ZipInputStream(fin);
                ZipEntry ze = null;
                while ((ze = zin.getNextEntry()) != null) {

                    LogUtil.logD(Constants.IS_DEBUG_MODE, TAG, "Unzipping " + ze.getName());
                    if (ze.isDirectory()) {
                        dirChecker(ze.getName());
                    } else {
                        // Update of my progress bar
                        LogUtil.logD(Constants.IS_DEBUG_MODE, TAG, "more " + ze.getName());

                        per++;
                        publishProgress(per);

                        FileOutputStream fout = new FileOutputStream(mLocation + ze.getName());
                        streamCopy(zin, fout);
                        zin.closeEntry();
                        fout.close();
                    }
                }
                zin.close();
            } catch (Exception e) {
                // Log.e("Decompress", "unzip", e);
                LogUtil.logE(Constants.IS_DEBUG_MODE, TAG, "unzip " + e);
            }
            return null;
        }

        @Override
        protected void onProgressUpdate(final Integer... values) {
            super.onProgressUpdate(values);
            mProgressDialog.setProgress(per);
        }

        @Override
        protected void onPostExecute(final Integer result) {
            super.onPostExecute(result);
            // Log.i("UnZip" ,"Completed. Total size: "+result);
            if (mProgressDialog.isShowing()) {
                mProgressDialog.dismiss();
            }
        }
    }

    /**
     * This is a method universally useful standard code to copy inputstreams to outputstreams.
     * @param in
     *            inputstream
     * @param out
     *            outputstream
     * @throws IOException
     *             exception
     */
    public static void streamCopy(final InputStream in, final OutputStream out) throws IOException {
        // play with sizes..
        byte[] buffer = new byte[Constants.NUMBER_THIRTY_TWO * Constants.NUMBER_1024];
        int readCount;
        while ((readCount = in.read(buffer)) != -1) {
            out.write(buffer, 0, readCount);
        }
    }

0 comments:

Post a Comment