Reading files from a ZIP file in your Android assets folder

后端 未结 3 1728
挽巷
挽巷 2020-12-15 08:46

I\'m reading files from a ZIP file that\'s located in my Android assets folder using ZipInputStream: it works, but it\'s really slow, as it has to read it seque

相关标签:
3条回答
  • 2020-12-15 09:12

    You can create a ZipInputStream in the following way :

    ZipInputStream zipIs = new ZipInputStream(context.getResources().openRawResource(your.package.com.R.raw.filename)); 
    ZipEntry ze = null;
    
            while ((ze = zipIs.getNextEntry()) != null) {
    
                FileOutputStream fout = new FileOutputStream(FOLDER_NAME +"/"+ ze.getName());
    
                byte[] buffer = new byte[1024];
                int length = 0;
    
                while ((length = zipIs.read(buffer))>0) {
                fout.write(buffer, 0, length);
                }
                zipIs .closeEntry();
                fout.close();
            }
            zipIs .close();
    
    0 讨论(0)
  • 2020-12-15 09:17

    You can store the uncompressed files directly in assets (i.e. unpack the zip into assets/ folder). This way, you can access the files directly and they will be compressed anyway when you build the APK.

    0 讨论(0)
  • 2020-12-15 09:23

    This works for me:

    private void loadzip(String folder, InputStream inputStream) throws IOException
    {
        ZipInputStream zipIs = new ZipInputStream(inputStream); 
        ZipEntry ze = null;
    
                while ((ze = zipIs.getNextEntry()) != null) {
    
                    FileOutputStream fout = new FileOutputStream(folder +"/"+ ze.getName());
    
                    byte[] buffer = new byte[1024];
                    int length = 0;
    
                    while ((length = zipIs.read(buffer))>0) {
                    fout.write(buffer, 0, length);
                    }
                    zipIs.closeEntry();
                    fout.close();
                }
                zipIs.close();
    }
    
    0 讨论(0)
提交回复
热议问题