如何解压缩包含图像的Zip文件而不删除android中的任何图像?

时间:2014-01-13 10:35:43

标签: java android zip unzip

我正在解压缩包含图像的zip文件,仅在此处给出了我的代码。

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();   }  }

但是我不知道解压缩一些图像正在丢弃的文件时发生了什么错误。我搜索了许多类型的代码进行解压缩但没有任何效果。

2 个答案:

答案 0 :(得分:3)

此代码解决了我的问题

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());
            }

        }

答案 1 :(得分:2)

看看http://www.lingala.net/zip4j/download.php,这个库文件快速而且很好。

您有使用库here

的简短说明

仅供参考:Here是另一个参考资料。

关于你的代码,我已经在我的项目中尝试了这个片段,但在解压缩时忽略了小图像,所以我使用了上面的库。现在我可以重新解决它。