Android Zip Unzip File unJarDirectory(final String in, final File fOut)

Here you can find the source of unJarDirectory(final String in, final File fOut)

Description

un Jar Directory

License

Apache License

Declaration

public static void unJarDirectory(final String in, final File fOut)
            throws IOException 

Method Source Code

//   Licensed under the Apache License, Version 2.0 (the "License");

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.jar.JarInputStream;
import java.util.zip.ZipEntry;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class Main{
    private static final Logger logger = LoggerFactory
            .getLogger(FileUtil.class);
    private static final int BUFFER = 1024;
    public static void unJarDirectory(final String in, final File fOut)
            throws IOException {
        if (!fOut.exists()) {
            fOut.mkdirs();/*from www.  j av  a  2  s.c  om*/
        }
        if (!fOut.isDirectory()) {
            throw new IOException("Destination must be a directory.");
        }
        if (logger.isDebugEnabled()) {
            logger.debug("Extracting Grinder Analyzer to " + fOut.getPath()
                    + "...");
        }

        final JarInputStream jin = new JarInputStream(new FileInputStream(
                in));
        final byte[] buffer = new byte[BUFFER];

        ZipEntry entry = jin.getNextEntry();
        while (entry != null) {
            String fileName = entry.getName();
            if (fileName.charAt(fileName.length() - 1) == '/') {
                fileName = fileName.substring(0, fileName.length() - 1);
            }
            if (fileName.charAt(0) == '/') {
                fileName = fileName.substring(1);
            }
            if (File.separatorChar != '/') {
                fileName = fileName.replace('/', File.separatorChar);
            }
            final File file = new File(fOut, fileName);
            if (entry.isDirectory()) {
                // make sure the directory exists
                file.mkdirs();
                jin.closeEntry();
            } else {
                // make sure the directory exists
                final File parent = file.getParentFile();
                if (parent != null && !parent.exists()) {
                    parent.mkdirs();
                }

                // dump the file
                final OutputStream out = new FileOutputStream(file);
                int len = 0;
                while ((len = jin.read(buffer, 0, buffer.length)) != -1) {
                    out.write(buffer, 0, len);
                }
                out.flush();
                out.close();
                jin.closeEntry();
                file.setLastModified(entry.getTime());
            }
            entry = jin.getNextEntry();
        }
        jin.close();
    }
}

Related

  1. gUnzip(File srcFile, File destFile)
  2. unzip(File pZipFile)
  3. unzip(String sourceFileName, String destPath)
  4. zip(String filePath, String zipPath)
  5. zip(String sourceDir, String zipFile)