How to unzip a Zip file which contains Images without dropping any image in android? [closed]

StackOverflow https://stackoverflow.com/questions/21089134

  •  27-09-2022
  •  | 
  •  

Question

I am working on unzip the zip file which contains Images only here below my code is given.

public void unZipImages(String zipFile, String outputFolder) {

  byte[] buffer = new byte[2048];

  try {

   // create output directory is not exists    
  File folder = new File(Environment.getExternalStorageDirectory()
     + "/final_unzip_data/" + AppConstants.ManufacturerCode
     + "/Catalog/" + "");    if (!folder.exists()) {
    folder.mkdir();    }

   // get the zip file content   
    ZipInputStream zis = new ZipInputStream(
     new FileInputStream(zipFile));    // get the zipped file list entry    ZipEntry ze = zis.getNextEntry();

   while (ze != null) {

    String fileName = ze.getName();
    if (fileName.contains("\\")) {
     fileName = fileName.replace("\\", "/");
    }
    File newFile = new File(outputFolder + File.separator
      + fileName);

    System.out.println("file unzip : " + newFile.getAbsoluteFile());

    new File(newFile.getParent()).mkdirs();

    FileOutputStream fos = new FileOutputStream(newFile);

    int len;
    while ((len = zis.read(buffer)) > 0) {
     fos.write(buffer, 0, len);
    }

    fos.close();
    ze = zis.getNextEntry();    }

   zis.closeEntry();    zis.close();

   System.out.println("Done");

  } catch (IOException ex) {    ex.printStackTrace();   }  }

But I don't know what the error is occurring to unzip the file some images are dropping. I have searched many types of codes for unzip but nothing worked.

Était-ce utile?

La solution

This code has solved my problem

private void unzipImage(String zipFile, String extractFolder) {

            try {
                CreateDir();
                int BUFFER = 4096;
                File file = new File(zipFile);

                ZipFile zip = new ZipFile(file);
                String newPath = extractFolder;

                new File(newPath).mkdir();
                Enumeration zipFileEntries = zip.entries();

                // Process each entry
                while (zipFileEntries.hasMoreElements()) {
                    // grab a zip file entry

                    ZipEntry entry = (ZipEntry) zipFileEntries.nextElement();
                    String currentEntry = entry.getName();

                    currentEntry = currentEntry.replace('\\', '/');
                    File destFile = new File(newPath, currentEntry);
                    // destFile = new File(newPath, destFile.getName());
                    File destinationParent = destFile.getParentFile();

                    // create the parent directory structure if needed
                    destinationParent.mkdirs();

                    if (!entry.isDirectory()) {
                        BufferedInputStream is = new BufferedInputStream(
                                zip.getInputStream(entry));
                        int currentByte;
                        // establish buffer for writing file
                        byte data[] = new byte[BUFFER];

                        // write the current file to disk
                        FileOutputStream fos = new FileOutputStream(destFile);
                        BufferedOutputStream dest = new BufferedOutputStream(fos,
                                BUFFER);

                        // read and write until last byte is encountered
                        while ((currentByte = is.read(data, 0, BUFFER)) != -1) {
                            dest.write(data, 0, currentByte);
                        }
                        dest.flush();
                        dest.close();
                        is.close();
                    }
                    zip.close();
                }
            } catch (Exception e) {
                Log.e("ERROR: ", "" + e.getMessage());
            }

        }

Autres conseils

Just have a look at http://www.lingala.net/zip4j/download.php , this library file is quick and good .

And you have the short explanation of using the library here

FYI : Here is the another references .

And regarding your code , I have also tried this snippet in my project already but it was ignoring small images while unzipping , so i used the above library . Now i could get it re-solved .

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top