Search in sources :

Example 76 with ZipFile

use of java.util.zip.ZipFile in project Lazy by l123456789jy.

the class ZipUtil method upZipFile.

/**
     * 解压缩一个文件
     *
     * @param zipFile 压缩文件
     * @param folderPath 解压缩的目标目录
     */
public static void upZipFile(File zipFile, String folderPath) {
    File desDir = new File(folderPath);
    if (!desDir.exists()) {
        desDir.mkdirs();
    }
    ZipFile zf = null;
    try {
        zf = new ZipFile(zipFile);
        for (Enumeration<?> entries = zf.entries(); entries.hasMoreElements(); ) {
            ZipEntry entry = ((ZipEntry) entries.nextElement());
            InputStream in = zf.getInputStream(entry);
            String str = folderPath + File.separator + entry.getName();
            str = new String(str.getBytes("8859_1"), "GB2312");
            File desFile = new File(str);
            if (!desFile.exists()) {
                File fileParentDir = desFile.getParentFile();
                if (!fileParentDir.exists()) {
                    fileParentDir.mkdirs();
                }
                desFile.createNewFile();
            }
            OutputStream out = new FileOutputStream(desFile);
            byte[] buffer = new byte[BUFF_SIZE];
            int realLength;
            while ((realLength = in.read(buffer)) > 0) {
                out.write(buffer, 0, realLength);
            }
            in.close();
            out.close();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : ZipFile(java.util.zip.ZipFile) BufferedInputStream(java.io.BufferedInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) ZipEntry(java.util.zip.ZipEntry) OutputStream(java.io.OutputStream) ZipOutputStream(java.util.zip.ZipOutputStream) FileOutputStream(java.io.FileOutputStream) BufferedOutputStream(java.io.BufferedOutputStream) FileOutputStream(java.io.FileOutputStream) File(java.io.File) ZipFile(java.util.zip.ZipFile) IOException(java.io.IOException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 77 with ZipFile

use of java.util.zip.ZipFile in project Lazy by l123456789jy.

the class ZipUtil method zipFiles.

/**
     * 批量压缩文件(夹)
     *
     * @param resFileList 要压缩的文件(夹)列表
     * @param zipFile 生成的压缩文件
     * @param zipListener     zipListener
     */
public static void zipFiles(Collection<File> resFileList, File zipFile, ZipListener zipListener) {
    ZipOutputStream zipout = null;
    try {
        zipout = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(zipFile), BUFF_SIZE));
        for (File resFile : resFileList) {
            if (stopZipFlag) {
                break;
            }
            zipFile(resFile, zipout, "", zipListener);
        }
        zipout.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : ZipOutputStream(java.util.zip.ZipOutputStream) FileOutputStream(java.io.FileOutputStream) BufferedOutputStream(java.io.BufferedOutputStream) File(java.io.File) ZipFile(java.util.zip.ZipFile) IOException(java.io.IOException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 78 with ZipFile

use of java.util.zip.ZipFile in project SmartAndroidSource by jaychou2012.

the class ZipUtil method iterate.

/**
	 * Reads the given ZIP file and executes the given action for each given
	 * entry.
	 * <p>
	 * For each given entry the corresponding input stream is also passed to the
	 * action. If you want to stop the loop then throw a ZipBreakException.
	 * 
	 * @param zip
	 *            input ZIP file.
	 * @param entryNames
	 *            names of entries to iterate
	 * @param action
	 *            action to be called for each entry.
	 * 
	 * @see ZipEntryCallback
	 * @see #iterate(File, String[], ZipInfoCallback)
	 */
public static void iterate(File zip, String[] entryNames, ZipEntryCallback action) {
    ZipFile zf = null;
    try {
        zf = new ZipFile(zip);
        for (int i = 0; i < entryNames.length; i++) {
            ZipEntry e = zf.getEntry(entryNames[i]);
            if (e == null) {
                continue;
            }
            InputStream is = zf.getInputStream(e);
            try {
                action.process(is, e);
            } catch (IOException ze) {
                throw new ZipException("Failed to process zip entry '" + e.getName() + " with action " + action, ze);
            } catch (ZipBreakException ex) {
                break;
            } finally {
                IOUtils.closeQuietly(is);
            }
        }
    } catch (IOException e) {
        throw ZipExceptionUtil.rethrow(e);
    } finally {
        closeQuietly(zf);
    }
}
Also used : ZipFile(java.util.zip.ZipFile) BufferedInputStream(java.io.BufferedInputStream) ZipInputStream(java.util.zip.ZipInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) ZipEntry(java.util.zip.ZipEntry) IOException(java.io.IOException)

Example 79 with ZipFile

use of java.util.zip.ZipFile in project SmartAndroidSource by jaychou2012.

the class Zips method iterateExistingExceptRemoved.

// ///////////// private api ///////////////
/**
	 * Iterate through source for not removed entries with a given callback
	 * 
	 * @param zipEntryCallback
	 *            callback to execute on entries or their info.
	 */
private void iterateExistingExceptRemoved(ZipEntryOrInfoAdapter zipEntryCallback) {
    if (src == null) {
        // iterate.
        return;
    }
    final Set<String> removedDirs = ZipUtil.filterDirEntries(src, removedEntries);
    ZipFile zf = null;
    try {
        zf = getZipFile();
        // manage existing entries
        Enumeration<? extends ZipEntry> en = zf.entries();
        while (en.hasMoreElements()) {
            ZipEntry entry = en.nextElement();
            String entryName = entry.getName();
            if (removedEntries.contains(entryName) || isEntryInDir(removedDirs, entryName)) {
                // removed entries are
                continue;
            }
            if (nameMapper != null) {
                String mappedName = nameMapper.map(entry.getName());
                if (mappedName == null) {
                    // we should ignore this entry
                    continue;
                } else if (!mappedName.equals(entry.getName())) {
                    // if name is different, do nothing
                    entry = ZipEntryUtil.copy(entry, mappedName);
                }
            }
            InputStream is = zf.getInputStream(entry);
            try {
                zipEntryCallback.process(is, entry);
            } catch (ZipBreakException ex) {
                break;
            } finally {
                IOUtils.closeQuietly(is);
            }
        }
    } catch (IOException e) {
        ZipExceptionUtil.rethrow(e);
    } finally {
        ZipUtil.closeQuietly(zf);
    }
}
Also used : ZipFile(java.util.zip.ZipFile) ZipInputStream(java.util.zip.ZipInputStream) PipedInputStream(java.io.PipedInputStream) InputStream(java.io.InputStream) ZipEntry(java.util.zip.ZipEntry) IOException(java.io.IOException)

Example 80 with ZipFile

use of java.util.zip.ZipFile in project SmartAndroidSource by jaychou2012.

the class ZipUtil method iterate.

/* Traversing ZIP files */
/**
	 * Reads the given ZIP file and executes the given action for each entry.
	 * <p>
	 * For each entry the corresponding input stream is also passed to the
	 * action. If you want to stop the loop then throw a ZipBreakException.
	 * 
	 * @param zip
	 *            input ZIP file.
	 * @param action
	 *            action to be called for each entry.
	 * 
	 * @see ZipEntryCallback
	 * @see #iterate(File, ZipInfoCallback)
	 */
public static void iterate(File zip, ZipEntryCallback action) {
    ZipFile zf = null;
    try {
        zf = new ZipFile(zip);
        Enumeration<? extends ZipEntry> en = zf.entries();
        while (en.hasMoreElements()) {
            ZipEntry e = (ZipEntry) en.nextElement();
            InputStream is = zf.getInputStream(e);
            try {
                action.process(is, e);
            } catch (IOException ze) {
                throw new ZipException("Failed to process zip entry '" + e.getName() + "' with action " + action, ze);
            } catch (ZipBreakException ex) {
                break;
            } finally {
                IOUtils.closeQuietly(is);
            }
        }
    } catch (IOException e) {
        throw ZipExceptionUtil.rethrow(e);
    } finally {
        closeQuietly(zf);
    }
}
Also used : ZipFile(java.util.zip.ZipFile) BufferedInputStream(java.io.BufferedInputStream) ZipInputStream(java.util.zip.ZipInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) ZipEntry(java.util.zip.ZipEntry) IOException(java.io.IOException)

Aggregations

ZipFile (java.util.zip.ZipFile)637 ZipEntry (java.util.zip.ZipEntry)454 File (java.io.File)287 IOException (java.io.IOException)214 InputStream (java.io.InputStream)147 FileOutputStream (java.io.FileOutputStream)108 ZipOutputStream (java.util.zip.ZipOutputStream)92 Test (org.junit.Test)89 FileInputStream (java.io.FileInputStream)68 Enumeration (java.util.Enumeration)47 ArrayList (java.util.ArrayList)46 BufferedInputStream (java.io.BufferedInputStream)44 BufferedOutputStream (java.io.BufferedOutputStream)39 ZipInputStream (java.util.zip.ZipInputStream)35 ZipException (java.util.zip.ZipException)34 OutputStream (java.io.OutputStream)31 ClassReader (org.objectweb.asm.ClassReader)29 FileNotFoundException (java.io.FileNotFoundException)26 JarFile (java.util.jar.JarFile)26 ByteArrayOutputStream (java.io.ByteArrayOutputStream)24