Search in sources :

Example 66 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 67 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 68 with ZipFile

use of java.util.zip.ZipFile in project dex-method-counts by mihaip.

the class Main method openDexFile.

RandomAccessFile openDexFile(ZipFile zipFile, ZipEntry entry) throws IOException {
    // We know it's a zip; see if there's anything useful inside.  A
    // failure here results in some type of IOException (of which
    // ZipException is a subclass).
    InputStream zis = zipFile.getInputStream(entry);
    // Create a temp file to hold the DEX data, open it, and delete it
    // to ensure it doesn't hang around if we fail.
    File tempFile = File.createTempFile("dexdeps", ".dex");
    RandomAccessFile dexFile = new RandomAccessFile(tempFile, "rw");
    tempFile.delete();
    // Copy all data from input stream to output file.
    byte[] copyBuf = new byte[32768];
    int actual;
    while (true) {
        actual = zis.read(copyBuf);
        if (actual == -1)
            break;
        dexFile.write(copyBuf, 0, actual);
    }
    dexFile.seek(0);
    return dexFile;
}
Also used : RandomAccessFile(java.io.RandomAccessFile) InputStream(java.io.InputStream) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File) ZipFile(java.util.zip.ZipFile)

Example 69 with ZipFile

use of java.util.zip.ZipFile in project dex-method-counts by mihaip.

the class Main method openInputFileAsZip.

/**
     * Tries to open an input file as a Zip archive (jar/apk) with a
     * "classes.dex" inside.
     */
void openInputFileAsZip(String fileName, List<RandomAccessFile> dexFiles) throws IOException {
    ZipFile zipFile;
    // Try it as a zip file.
    try {
        zipFile = new ZipFile(fileName);
    } catch (FileNotFoundException fnfe) {
        // not found, no point in retrying as non-zip.
        System.err.println("Unable to open '" + fileName + "': " + fnfe.getMessage());
        throw fnfe;
    } catch (ZipException ze) {
        // not a zip
        return;
    }
    // Open and add all files matching "classes.*\.dex" in the zip file.
    for (ZipEntry entry : Collections.list(zipFile.entries())) {
        if (entry.getName().matches("classes.*\\.dex")) {
            dexFiles.add(openDexFile(zipFile, entry));
        }
    }
    zipFile.close();
}
Also used : ZipFile(java.util.zip.ZipFile) ZipEntry(java.util.zip.ZipEntry) FileNotFoundException(java.io.FileNotFoundException) ZipException(java.util.zip.ZipException)

Example 70 with ZipFile

use of java.util.zip.ZipFile in project jodd by oblac.

the class ZipUtilTest method testZip.

@Test
public void testZip() throws IOException {
    ZipUtil.zip(new File(dataRoot, "sb.data"));
    File zipFile = new File(dataRoot, "sb.data.zip");
    assertTrue(zipFile.exists());
    // cleanup
    FileUtil.delete(zipFile);
    ZipUtil.zip(new File(dataRoot, "file"));
    zipFile = new File(dataRoot, "file.zip");
    assertTrue(zipFile.exists());
    // cleanup
    FileUtil.delete(zipFile);
}
Also used : File(java.io.File) ZipFile(java.util.zip.ZipFile) Test(org.junit.Test)

Aggregations

ZipFile (java.util.zip.ZipFile)580 ZipEntry (java.util.zip.ZipEntry)414 File (java.io.File)261 IOException (java.io.IOException)189 InputStream (java.io.InputStream)127 FileOutputStream (java.io.FileOutputStream)98 ZipOutputStream (java.util.zip.ZipOutputStream)89 Test (org.junit.Test)88 FileInputStream (java.io.FileInputStream)57 ArrayList (java.util.ArrayList)44 Enumeration (java.util.Enumeration)42 BufferedInputStream (java.io.BufferedInputStream)38 BufferedOutputStream (java.io.BufferedOutputStream)35 ZipException (java.util.zip.ZipException)32 ClassReader (org.objectweb.asm.ClassReader)29 OutputStream (java.io.OutputStream)27 JarFile (java.util.jar.JarFile)26 ZipInputStream (java.util.zip.ZipInputStream)26 FileNotFoundException (java.io.FileNotFoundException)23 Path (java.nio.file.Path)23