Example usage for java.util.zip ZipFile ZipFile

List of usage examples for java.util.zip ZipFile ZipFile

Introduction

In this page you can find the example usage for java.util.zip ZipFile ZipFile.

Prototype

public ZipFile(File file) throws ZipException, IOException 

Source Link

Document

Opens a ZIP file for reading given the specified File object.

Usage

From source file:Main.java

public static boolean upZipFile(File zipFile, String folderPath) throws ZipException, IOException {
    ZipFile zfile = new ZipFile(zipFile);
    Enumeration<? extends ZipEntry> zList = zfile.entries();
    ZipEntry ze = null;/*from   ww  w  . j a  v  a2s.  co  m*/
    byte[] buf = new byte[1024];
    while (zList.hasMoreElements()) {
        ze = (ZipEntry) zList.nextElement();
        if (ze.isDirectory()) {
            continue;
        }
        Log.d(TAG, "ze.getName() = " + ze.getName());
        OutputStream os = new BufferedOutputStream(
                new FileOutputStream(getRealFileName(folderPath, ze.getName())));
        InputStream is = new BufferedInputStream(zfile.getInputStream(ze));
        int readLen = 0;
        while ((readLen = is.read(buf, 0, 1024)) != -1) {
            os.write(buf, 0, readLen);
        }
        is.close();
        os.close();
    }
    zfile.close();
    return true;
}

From source file:Main.java

/**
 * To check if the apk file is available to install.
 * //  ww w .j  a  v a  2s  .c o m
 * @param apkFilePath the apk file path
 * @return            true if available, otherwise return false
 */
public static boolean isApkAvailable(String apkFilePath) {
    File apkFile = new File(apkFilePath);
    if (!apkFile.exists()) {
        return false;
    }

    try {
        ZipFile zipFile = new ZipFile(apkFile);
        Enumeration<? extends ZipEntry> entries = zipFile.entries();
        while (entries.hasMoreElements()) {
            ZipEntry zipEntry = entries.nextElement();
            if (APK_MANIFEST.equals(zipEntry.getName())) {
                zipFile.close();
                return true;
            }
        }

        zipFile.close();
    } catch (Exception e) {
        return false;
    }

    return false;
}

From source file:Main.java

/**
 * Get channel from META-INF directory./*w  ww. j  av  a2 s. co  m*/
 *
 * @return the channel name if success,otherwise return "none"
 */
public static String getChannel(Context context) {
    ApplicationInfo appinfo = context.getApplicationInfo();
    String sourceDir = appinfo.sourceDir;
    String ret = "";
    ZipFile zipfile = null;
    try {
        zipfile = new ZipFile(sourceDir);
        Enumeration<?> entries = zipfile.entries();
        while (entries.hasMoreElements()) {
            ZipEntry entry = ((ZipEntry) entries.nextElement());
            String entryName = entry.getName();
            if (entryName.startsWith("META-INF/channel")) {
                ret = entryName;
                break;
            }
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (zipfile != null) {
            try {
                zipfile.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    String[] split = ret.split("_");
    if (split != null && split.length >= 2) {
        return ret.substring(split[0].length() + 1);

    } else {
        return "none";
    }
}

From source file:Main.java

public static boolean searchJarFile(File file, String classFilePath) {

    try {//from   www  . j a  va  2s  .co  m
        if (!file.exists())
            return false;

        ZipFile jarFile = new ZipFile(file);
        if (jarFile.getEntry(classFilePath) != null) {
            jarFile.close();
            return true;
        } else {
            jarFile.close();
            return false;
        }
    } catch (IOException ex) {
        System.out.println(ex.toString());
        return false;
    }
}

From source file:Main.java

public static String getChannel(Context context) {
    ApplicationInfo appinfo = context.getApplicationInfo();
    String sourceDir = appinfo.sourceDir;
    ZipFile zipfile = null;/*from w ww.  j  av a 2s .co  m*/
    String ret = null;
    try {
        zipfile = new ZipFile(sourceDir);
        Enumeration<?> entries = zipfile.entries();
        while (entries.hasMoreElements()) {
            ZipEntry entry = ((ZipEntry) entries.nextElement());
            String entryName = entry.getName();
            if (entryName.startsWith(FOLDER_NAME) && entryName.contains(CHANNEL_SCHEME)) {
                ret = entryName;
                break;
            }
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (zipfile != null) {
            try {
                zipfile.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    try {
        if (ret != null) {
            String[] split = ret.split(CHANNEL_SCHEME_SPIT);
            if (split.length >= 2) {
                return ret.substring(split[0].length() + 1);
            }
        }
    } catch (Exception e) {

    }

    return null;
}

From source file:Main.java

/**
 * extracts a zip file to the given dir//from   w w w.j a v a2s . c  o  m
 * @param archive
 * @param destDir
 * @throws IOException 
 * @throws ZipException 
 * @throws Exception
 */
public static void extractZipArchive(File archive, File destDir) throws ZipException, IOException {
    if (!destDir.exists()) {
        destDir.mkdir();
    }

    ZipFile zipFile = new ZipFile(archive);
    Enumeration<? extends ZipEntry> entries = zipFile.entries();

    byte[] buffer = new byte[16384];
    int len;
    while (entries.hasMoreElements()) {
        ZipEntry entry = entries.nextElement();

        String entryFileName = entry.getName();

        File dir = buildDirectoryHierarchyFor(entryFileName, destDir);
        if (!dir.exists()) {
            dir.mkdirs();
        }

        if (!entry.isDirectory()) {
            BufferedOutputStream bos = new BufferedOutputStream(
                    new FileOutputStream(new File(destDir, entryFileName)));

            BufferedInputStream bis = new BufferedInputStream(zipFile.getInputStream(entry));

            while ((len = bis.read(buffer)) > 0) {
                bos.write(buffer, 0, len);
            }

            bos.flush();
            bos.close();
            bis.close();
        }
    }
}

From source file:net.bpelunit.util.ZipUtil.java

public static void unzipFile(File zip, File dir) throws IOException {
    InputStream in = null;/*from w w w.  ja  v  a  2  s  .c om*/
    OutputStream out = null;
    ZipFile zipFile = new ZipFile(zip);
    Enumeration<? extends ZipEntry> entries = zipFile.entries();

    while (entries.hasMoreElements()) {
        ZipEntry entry = entries.nextElement();
        if (!entry.getName().endsWith("/")) {
            File unzippedFile = new File(dir, entry.getName());
            try {
                in = zipFile.getInputStream(entry);
                unzippedFile.getParentFile().mkdirs();

                out = new FileOutputStream(unzippedFile);

                IOUtils.copy(in, out);
            } finally {
                IOUtils.closeQuietly(in);
                IOUtils.closeQuietly(out);
            }
        }
    }
}

From source file:Main.java

public static void unzip(URL _url, URL _dest, boolean _remove_top) {
    int BUFFER_SZ = 2048;
    File file = new File(_url.getPath());

    String dest = _dest.getPath();
    new File(dest).mkdir();

    try {//w  ww .j a va2  s  . c  om
        ZipFile zip = new ZipFile(file);
        Enumeration<? extends ZipEntry> zipFileEntries = zip.entries();

        // Process each entry
        while (zipFileEntries.hasMoreElements()) {
            // grab a zip file entry
            ZipEntry entry = (ZipEntry) zipFileEntries.nextElement();
            String currentEntry = entry.getName();

            if (_remove_top)
                currentEntry = currentEntry.substring(currentEntry.indexOf('/'), currentEntry.length());

            File destFile = new File(dest, 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;
                is = new BufferedInputStream(zip.getInputStream(entry));
                int currentByte;
                // establish buffer for writing file
                byte data[] = new byte[BUFFER_SZ];

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

                // read and write until last byte is encountered
                while ((currentByte = is.read(data, 0, BUFFER_SZ)) != -1) {
                    dst.write(data, 0, currentByte);
                }
                dst.flush();
                dst.close();
                is.close();
            }
            /*
            if (currentEntry.endsWith(".zip"))
            {
            // found a zip file, try to open
            extractFolder(destFile.getAbsolutePath());
            }*/
        }
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

From source file:Main.java

private static Path unzip(Path tmpDir, Path zipPath, String name) throws IOException {
    Path outPath = tmpDir.resolve(zipPath.getFileName());
    try (ZipFile zipFile = new ZipFile(zipPath.toFile())) {
        Files.copy(zipFile.getInputStream(zipFile.getEntry(name)), outPath,
                StandardCopyOption.REPLACE_EXISTING);
        return outPath;
    }/*from w w  w  .j ava  2s . c  o m*/
}

From source file:com.oneis.app.FileController.java

public static ZipFileExtractionResults zipFileExtraction(String zipFilePathname, String contentsDefault,
        String contentsRequested) throws IOException {
    ZipFileExtractionResults results = new ZipFileExtractionResults();

    ZipFile zipFile = new ZipFile(zipFilePathname);
    try {//from   w  ww. j a  v  a2  s. c o m
        // Find the right entry, trying for the requested filename by defaulting back on the default given
        ZipEntry e = zipFile.getEntry(contentsRequested);
        results.chosenFile = contentsRequested;
        if (e == null) {
            e = zipFile.getEntry(contentsDefault);
            results.chosenFile = contentsDefault;
        }
        if (e == null) {
            return null;
        }

        results.data = IOUtils.toByteArray(zipFile.getInputStream(e));
    } finally {
        zipFile.close();
    }

    results.etagSuggestion = results.chosenFile.hashCode() & 0xfffff;

    return results;
}