How to copy content from a zip to an android directory?

1

Hello everyone.

Ask this question so that you can give me a hand and guide me on my way.

My problem

I want to be able to decompress a zip in a folder or directory on the sdcard, but my code has not hit the target. Its flaw is that it does not decompress or copy any of the files inside it. The zip file is located in the Assets resource folder.

My code

    private boolean copyFile1(String filename1, String outPath1) {
    AssetManager assetManager = this.getAssets();
    final int CHUNK_SIZE = 1024 * 4;
    InputStream in;
    OutputStream out;
    try {
        in = assetManager.open(filename1);
        String newFileName = outPath1;

        ZipInputStream zipStream = new ZipInputStream(in);
        ZipEntry zEntry = null;
        while ((zEntry = zipStream.getNextEntry()) != null) {


            if (zEntry.isDirectory()) {

            } else {
                FileOutputStream fout = new FileOutputStream(new File(outPath1));
                BufferedOutputStream bufout = new BufferedOutputStream(fout);
                byte[] buffer = new byte[CHUNK_SIZE];
                int read = 0;
                while ((read = zipStream.read(buffer)) != -1) {
                    bufout.write(buffer, 0, read);
                }

                zipStream.closeEntry();
                bufout.close();
                fout.close();
            }
        }
        zipStream.close();
        Log.d("Unzip", "Unzipping complete. path :  " );

    } catch (Exception e) {
        Log.e("TAG", e.getMessage());
    }
    return true;

}

If they notice where they fail or know another way. Please communicate it to me. Thanks

    
asked by Abraham.P 09.02.2017 в 22:26
source

2 answers

2

If you have defined the permission and you use android 6+ besides adding the permission to your AndroidManifest.xml :

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

You must manually require permissions by:

private static final int REQUEST_CODE_ASK_PERMISSIONS = 507;

    private void checkWritePermission(){
        if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
            // No action!.
        }else{
            ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, REQUEST_CODE_ASK_PERMISSIONS);
            return;
        }
    }


    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        switch (requestCode){
            case REQUEST_CODE_ASK_PERMISSIONS:
                if(grantResults[0] != PackageManager.PERMISSION_GRANTED){
                    // Permiso negado.                                  
                }
            default:
                super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        }
    }

Asked about the routes you send to your method, the paths of the files must be defined within the external storage, for example:

String archivoZip = Environment.getExternalStorageDirectory() + "/Android/data/archivoszip/miarchivo.zip";
String directorioArchivosDecompresos  = Environment.getExternalStorageDirectory() + "/Android/data/archivosdescomprimidos/"; 

Copy content from a .zip to a directory on android.

For this you have to unzip and add this class that will be useful:

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import android.util.Log;

public class Decompressfile {

    private String zipFile;
    private String decompresslocation;

    public Decompressfile(String zipFile, String location) {
        this.zipFile = zipFile;
        this.decompresslocation = location;

        _dirChecker("");
    }

    public void unzip() {
        try  {
            FileInputStream fin = new FileInputStream(zipFile);
            ZipInputStream zin = new ZipInputStream(fin);
            ZipEntry ze = null;
            while ((ze = zin.getNextEntry()) != null) {
                Log.v("Decompress", "Unzipping " + ze.getName());

                if(ze.isDirectory()) {
                    _dirChecker(ze.getName());
                } else {
                    FileOutputStream fout = new FileOutputStream(decompresslocation + ze.getName());
                    BufferedOutputStream bufout = new BufferedOutputStream(fout);
                    byte[] buffer = new byte[1024];
                    int read = 0;
                    while ((read = zin.read(buffer)) != -1) {
                        bufout.write(buffer, 0, read);
                    }




                    bufout.close();

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

            }
            zin.close();


            Log.d("Unzip", "Unzipping complete. path :  " + decompresslocation);
        } catch(Exception e) {
            Log.e("Decompress", "unzip", e);

            Log.d("Unzip", "Unzipping failed");
        }

    }

    private void _dirChecker(String dir) {
        File f = new File(decompresslocation + dir);

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

}

To use it would be like this:

    String archivoZip = Environment.getExternalStorageDirectory() + "/Android/data/archivoszip/miarchivo.zip";
    String directorioArchivosDecompresos = Environment.getExternalStorageDirectory() + "/Android/data/archivosdescomprimidos/"; 

new Decompressfile(archivoZip, directorioArchivosDecompresos).unzip();
    
answered by 10.02.2017 / 19:19
source
0

It may be a permission error. Check that you have the following line included in your AndroidManifest.xml:

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

On the other hand, I do not know how you pass the path to your method, but you should do it on a route whose base was obtained like this:

Environment.getExternalStorageDirectory();
    
answered by 10.02.2017 в 08:50