Question

I'm sure that there is a mature, widely used ZIP file utility out there, I just can't seem to find out. Something with the same maturity as Apache Commons, Google Collections, Joda Time

I'm trying to do the simplest task of getting a zip file as a byte array (ZipInputStream) and extract it to a folder. this seems like a very tedious task.

I would hope for a syntactic sugar API that does somethnig like this:

public class MyDreamZIPUtils 
      public static void extractToFolder(ZipInputStream zin, File outputFolderRoot){
           ...
      }
      public static void extractToFolder(ZipFile zf, File outputFolderRoot){
           ...
      }

      public static zipFolder(File folderToZip, File zippedFileLocation){
           ...
      }

      public static zipFolder(File folderToZip, ByteArrayOutputStream zipResult){
           ...
      }

Anything like this? Am I missing something?

Was it helpful?

Solution

http://commons.apache.org/compress/

I am sure you can write the "syntactic sugar" on top of that.

Javadoc: http://commons.apache.org/compress/apidocs/index.html

OTHER TIPS

I used only Java API calls... I did not do all your methods. you can figure them out from here... Please note i do not claim that the code is bug free... use at your own risk :)

    public static void extractToFolder(ZipInputStream zin, File outputFolderRoot)
                throws IOException {

            FileOutputStream fos = null;
            byte[] buf = new byte[1024];
            ZipEntry zipentry;

            for (zipentry = zin.getNextEntry(); zipentry != null; zipentry = zin.getNextEntry()) {

                try {
                    String entryName = zipentry.getName();
                    System.out.println("Extracting: " + entryName);
                    int n;

                    File newFile = new File(outputFolderRoot, entryName);
                    if (zipentry.isDirectory()) {
                        newFile.mkdirs();
                        continue;
                    } else {
                        newFile.getParentFile().mkdirs();
                        newFile.createNewFile();
                    }

                    fos = new FileOutputStream(newFile);

                    while ((n = zin.read(buf, 0, 1024)) > -1)
                        fos.write(buf, 0, n);

                    fos.close();
                    zin.closeEntry();
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    if (fos != null)
                        try {
                            fos.close();
                        } catch (Exception ignore) {
                        }
                }

            }

            zin.close();

        }


    public static void zipFolder(File folderToZip, File zippedFileLocation) throws IOException {
        // create a ZipOutputStream to zip the data to
        ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(zippedFileLocation));
        String path = "";
        zipDir(folderToZip, zos, path);
        // close the stream
        zos.close();
    }

    private static void zipDir(File directory, ZipOutputStream zos, String path) throws IOException {
        File zipDir = directory;
        // get a listing of the directory content
        String[] dirList = zipDir.list();
        byte[] readBuffer = new byte[2156];
        int bytesIn = 0;
        // loop through dirList, and zip the files
        for (int i = 0; i < dirList.length; i++) {
            File f = new File(zipDir, dirList[i]);
            if (f.isDirectory()) {
                zipDir(new File(f.getPath()), zos, path + f.getName() + "/");
                continue;
            }
            FileInputStream fis = new FileInputStream(f);
            try {
                ZipEntry anEntry = new ZipEntry(path + f.getName());
                zos.putNextEntry(anEntry);
                bytesIn = fis.read(readBuffer);
                while (bytesIn != -1) {
                    zos.write(readBuffer, 0, bytesIn);
                    bytesIn = fis.read(readBuffer);
                }
            } finally {
                fis.close();
            }
        }
    }

Reference Java2s

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top