Skip to content Skip to sidebar Skip to footer

How To Zip And Unzip Png Images In Android

Hi in my app when i click zip button i need to zip image file and and when i click unzip button i need to unzip file,i tried using below code to zip image but my problem is when i

Solution 1:

You can check out these 2 tutorials to zip and unzip the file

Android Zip file

Android Unzip file


Solution 2:

You can use ZipOutputStream in Java

See API Document Here

You can create the zip file by follow function

 OutputStream os = ...
 ZipOutputStream zos = new ZipOutputStream(new BufferedOutputStream(os));
 try {
     for (int i = 0; i < fileCount; ++i) {
         String filename = ...
         byte[] bytes = ...
         ZipEntry entry = new ZipEntry(filename);
         zos.putNextEntry(entry);
         zos.write(bytes);
         zos.closeEntry();
     }
 } finally {
     zos.close();
 }

For Open a zip file, you can use ZipInputStream Api Document here

InputStream is = ...
 ZipInputStream zis = new ZipInputStream(new BufferedInputStream(is));
 try {
     ZipEntry ze;
     while ((ze = zis.getNextEntry()) != null) {
         ByteArrayOutputStream baos = new ByteArrayOutputStream();
         byte[] buffer = new byte[1024];
         int count;
         while ((count = zis.read(buffer)) != -1) {
             baos.write(buffer, 0, count);
         }
         String filename = ze.getName();
         byte[] bytes = baos.toByteArray();
         // do something with 'filename' and 'bytes'...
     }
 } finally {
     zis.close();
 }

Post a Comment for "How To Zip And Unzip Png Images In Android"