简体   繁体   中英

How to unzip code on nexus7

How to unzip programming android from asset to /sdcard/, my code in below not success in nexus7, but if it is run in addition to nexus7 will be fine, will be able to extract the data.

To running unzip.

new Thread(new Runnable() {
    @Override
    public void run() {
        UnZip.start();
    }
}).start();


 private Thread UnZip = new Thread() {
            @Override
        public void run() {
            try {
                final int BUFFER = 8192;

                ZipInputStream inputStream = new ZipInputStream(getAssets().open("file.zip"));

                for (ZipEntry entry = inputStream.getNextEntry(); entry != null; entry = inputStream
                        .getNextEntry()) {

                    String innerFileName = "/sdcard/" + File.separator
                            + entry.getName();
                    File innerFile = new File(innerFileName);

                    if (entry.isDirectory()) {
                        innerFile.mkdirs();
                    } else {
                        FileOutputStream outputStream = new FileOutputStream(
                                innerFileName);

                        BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(
                                outputStream, BUFFER);

                        int count = 0;
                        byte[] data = new byte[BUFFER];
                        while ((count = inputStream.read(data, 0, BUFFER)) != -1) {
                            bufferedOutputStream.write(data, 0, count);
                        }

                        bufferedOutputStream.flush();
                        bufferedOutputStream.close();
                    }
                }
                inputStream.closeEntry();
                inputStream.close();

                     } catch (Exception e) {  

                 }
            }
        };

instead of look for sdcard in this way

 String innerFileName = "/sdcard/" + File.separator
                            + entry.getName();

use the Environment API

  String innerFileName = Environment.getExternalStorageDirectory().toString() + File.separator + entry.getName();

do not forget to add the WRITE_EXTERNAL_STORAGE permission to your AndroidManifest.xml

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM