Search in sources :

Example 21 with ZipOutputStream

use of java.util.zip.ZipOutputStream in project tinker by Tencent.

the class FileUtil method differZip.

/**
     * differ zip
     *
     * @param differentOutputFullFilename
     * @param oldZipFullFilename
     * @param newZipFullFilename
     */
public static void differZip(String differentOutputFullFilename, String oldZipFullFilename, String newZipFullFilename) {
    Map<String, String> map = getZipEntryHashMap(oldZipFullFilename);
    ZipFile newZipFile = null;
    ZipOutputStream zipOutputStream = null;
    try {
        newZipFile = new ZipFile(newZipFullFilename);
        Enumeration<? extends ZipEntry> entries = newZipFile.entries();
        FileUtil.createFile(differentOutputFullFilename);
        zipOutputStream = new ZipOutputStream(new FileOutputStream(differentOutputFullFilename));
        while (entries.hasMoreElements()) {
            ZipEntry zipEntry = entries.nextElement();
            if (!zipEntry.isDirectory()) {
                String zipEntryName = zipEntry.getName();
                String oldZipEntryHash = map.get(zipEntryName);
                String newZipEntryHash = zipEntry.getCrc() + Constant.Symbol.DOT + zipEntry.getSize();
                // is a modified zip entry
                if (oldZipEntryHash == null || (!newZipEntryHash.equals(oldZipEntryHash))) {
                    System.out.println(String.format("found modified entry, key=%s(%s/%s)", new Object[] { zipEntryName, oldZipEntryHash, newZipEntryHash }));
                    addZipEntry(zipOutputStream, zipEntry, newZipFile.getInputStream(zipEntry));
                }
            }
        }
    } catch (Exception e) {
        throw new FileUtilException(e);
    } finally {
        if (newZipFile != null) {
            try {
                newZipFile.close();
            } catch (IOException e) {
                throw new FileUtilException(e);
            }
        }
        if (zipOutputStream != null) {
            try {
                zipOutputStream.finish();
            } catch (IOException e) {
                throw new FileUtilException(e);
            }
        }
    }
}
Also used : ZipFile(java.util.zip.ZipFile) ZipOutputStream(java.util.zip.ZipOutputStream) FileOutputStream(java.io.FileOutputStream) ZipEntry(java.util.zip.ZipEntry) IOException(java.io.IOException) ZipException(java.util.zip.ZipException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException)

Example 22 with ZipOutputStream

use of java.util.zip.ZipOutputStream in project tinker by Tencent.

the class FileUtil method mergeZip.

/**
     * merge zip file
     *
     * @param zipOutputFullFilename
     * @param mergeZipFullFilenameList
     */
public static void mergeZip(String zipOutputFullFilename, List<String> mergeZipFullFilenameList) {
    FileUtil.createFile(zipOutputFullFilename);
    ZipOutputStream zipOutputStream = null;
    try {
        zipOutputStream = new ZipOutputStream(new FileOutputStream(zipOutputFullFilename));
        if (mergeZipFullFilenameList != null) {
            for (String zipFullFilename : mergeZipFullFilenameList) {
                if (isExist(zipFullFilename)) {
                    ZipFile zipFile = new ZipFile(zipFullFilename);
                    Enumeration<? extends ZipEntry> enumeration = zipFile.entries();
                    while (enumeration.hasMoreElements()) {
                        ZipEntry zipEntry = enumeration.nextElement();
                        InputStream inputStream = zipFile.getInputStream(zipEntry);
                        addZipEntry(zipOutputStream, zipEntry, inputStream);
                    }
                    zipFile.close();
                }
            }
        }
    } catch (Exception e) {
        throw new FileUtilException(e);
    } finally {
        try {
            if (zipOutputStream != null) {
                zipOutputStream.close();
            }
        } catch (Exception e) {
            throw new FileUtilException(e);
        }
    }
}
Also used : ZipFile(java.util.zip.ZipFile) ZipOutputStream(java.util.zip.ZipOutputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) FileOutputStream(java.io.FileOutputStream) ZipEntry(java.util.zip.ZipEntry) ZipException(java.util.zip.ZipException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException)

Example 23 with ZipOutputStream

use of java.util.zip.ZipOutputStream in project MinecraftForge by MinecraftForge.

the class AccessTransformer method processJar.

private static void processJar(File inFile, File outFile, AccessTransformer[] transformers) throws IOException {
    ZipInputStream inJar = null;
    ZipOutputStream outJar = null;
    try {
        try {
            inJar = new ZipInputStream(new BufferedInputStream(new FileInputStream(inFile)));
        } catch (FileNotFoundException e) {
            throw new FileNotFoundException("Could not open input file: " + e.getMessage());
        }
        try {
            outJar = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(outFile)));
        } catch (FileNotFoundException e) {
            throw new FileNotFoundException("Could not open output file: " + e.getMessage());
        }
        ZipEntry entry;
        while ((entry = inJar.getNextEntry()) != null) {
            if (entry.isDirectory()) {
                outJar.putNextEntry(entry);
                continue;
            }
            byte[] data = new byte[4096];
            ByteArrayOutputStream entryBuffer = new ByteArrayOutputStream();
            int len;
            do {
                len = inJar.read(data);
                if (len > 0) {
                    entryBuffer.write(data, 0, len);
                }
            } while (len != -1);
            byte[] entryData = entryBuffer.toByteArray();
            String entryName = entry.getName();
            if (entryName.endsWith(".class") && !entryName.startsWith(".")) {
                ClassNode cls = new ClassNode();
                ClassReader rdr = new ClassReader(entryData);
                rdr.accept(cls, 0);
                String name = cls.name.replace('/', '.').replace('\\', '.');
                for (AccessTransformer trans : transformers) {
                    entryData = trans.transform(name, name, entryData);
                }
            }
            ZipEntry newEntry = new ZipEntry(entryName);
            outJar.putNextEntry(newEntry);
            outJar.write(entryData);
        }
    } finally {
        IOUtils.closeQuietly(outJar);
        IOUtils.closeQuietly(inJar);
    }
}
Also used : ClassNode(org.objectweb.asm.tree.ClassNode) ZipEntry(java.util.zip.ZipEntry) FileNotFoundException(java.io.FileNotFoundException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) FileInputStream(java.io.FileInputStream) ZipInputStream(java.util.zip.ZipInputStream) BufferedInputStream(java.io.BufferedInputStream) ZipOutputStream(java.util.zip.ZipOutputStream) FileOutputStream(java.io.FileOutputStream) ClassReader(org.objectweb.asm.ClassReader) BufferedOutputStream(java.io.BufferedOutputStream)

Example 24 with ZipOutputStream

use of java.util.zip.ZipOutputStream in project MinecraftForge by MinecraftForge.

the class MarkerTransformer method processJar.

private static void processJar(File inFile, File outFile, MarkerTransformer[] transformers) throws IOException {
    ZipInputStream inJar = null;
    ZipOutputStream outJar = null;
    try {
        try {
            inJar = new ZipInputStream(new BufferedInputStream(new FileInputStream(inFile)));
        } catch (FileNotFoundException e) {
            throw new FileNotFoundException("Could not open input file: " + e.getMessage());
        }
        try {
            outJar = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(outFile)));
        } catch (FileNotFoundException e) {
            throw new FileNotFoundException("Could not open output file: " + e.getMessage());
        }
        ZipEntry entry;
        while ((entry = inJar.getNextEntry()) != null) {
            if (entry.isDirectory()) {
                outJar.putNextEntry(entry);
                continue;
            }
            byte[] data = new byte[4096];
            ByteArrayOutputStream entryBuffer = new ByteArrayOutputStream();
            int len;
            do {
                len = inJar.read(data);
                if (len > 0) {
                    entryBuffer.write(data, 0, len);
                }
            } while (len != -1);
            byte[] entryData = entryBuffer.toByteArray();
            String entryName = entry.getName();
            if (entryName.endsWith(".class") && !entryName.startsWith(".")) {
                ClassNode cls = new ClassNode();
                ClassReader rdr = new ClassReader(entryData);
                rdr.accept(cls, 0);
                String name = cls.name.replace('/', '.').replace('\\', '.');
                for (MarkerTransformer trans : transformers) {
                    entryData = trans.transform(name, name, entryData);
                }
            }
            ZipEntry newEntry = new ZipEntry(entryName);
            outJar.putNextEntry(newEntry);
            outJar.write(entryData);
        }
    } finally {
        IOUtils.closeQuietly(outJar);
        IOUtils.closeQuietly(inJar);
    }
}
Also used : ClassNode(org.objectweb.asm.tree.ClassNode) ZipEntry(java.util.zip.ZipEntry) FileNotFoundException(java.io.FileNotFoundException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) FileInputStream(java.io.FileInputStream) ZipInputStream(java.util.zip.ZipInputStream) BufferedInputStream(java.io.BufferedInputStream) ZipOutputStream(java.util.zip.ZipOutputStream) FileOutputStream(java.io.FileOutputStream) ClassReader(org.objectweb.asm.ClassReader) BufferedOutputStream(java.io.BufferedOutputStream)

Example 25 with ZipOutputStream

use of java.util.zip.ZipOutputStream in project atlas by alibaba.

the class ZipUtils method addFileAndDirectoryToZip.

public static void addFileAndDirectoryToZip(File output, File srcDir, Map<String, ZipEntry> zipEntryMethodMap) throws Exception {
    if (output.isDirectory()) {
        throw new IOException("This is a directory!");
    }
    if (!output.getParentFile().exists()) {
        output.getParentFile().mkdirs();
    }
    if (!output.exists()) {
        output.createNewFile();
    }
    List fileList = getSubFiles(srcDir);
    ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(output));
    ZipEntry ze = null;
    byte[] buf = new byte[1024];
    int readLen = 0;
    for (int i = 0; i < fileList.size(); i++) {
        File f = (File) fileList.get(i);
        ze = new ZipEntry(getAbsFileName(srcDir.getPath(), f));
        ze.setSize(f.length());
        ze.setTime(f.lastModified());
        if (zipEntryMethodMap != null) {
            ZipEntry originEntry = zipEntryMethodMap.get(f.getAbsolutePath());
            if (originEntry != null) {
                ze.setCompressedSize(originEntry.getCompressedSize());
                ze.setCrc(originEntry.getCrc());
                ze.setMethod(originEntry.getMethod());
            }
        }
        zos.putNextEntry(ze);
        InputStream is = new BufferedInputStream(new FileInputStream(f));
        while ((readLen = is.read(buf, 0, 1024)) != -1) {
            zos.write(buf, 0, readLen);
        }
        is.close();
    }
    zos.close();
}
Also used : ZipOutputStream(java.util.zip.ZipOutputStream) ZipInputStream(java.util.zip.ZipInputStream) ZipEntry(java.util.zip.ZipEntry) ArrayList(java.util.ArrayList) List(java.util.List) ZipFile(org.apache.commons.compress.archivers.zip.ZipFile)

Aggregations

ZipOutputStream (java.util.zip.ZipOutputStream)463 ZipEntry (java.util.zip.ZipEntry)308 FileOutputStream (java.io.FileOutputStream)216 File (java.io.File)181 IOException (java.io.IOException)118 ZipFile (java.util.zip.ZipFile)104 BufferedOutputStream (java.io.BufferedOutputStream)90 ByteArrayOutputStream (java.io.ByteArrayOutputStream)83 FileInputStream (java.io.FileInputStream)82 Test (org.junit.Test)68 InputStream (java.io.InputStream)61 ZipInputStream (java.util.zip.ZipInputStream)44 ByteArrayInputStream (java.io.ByteArrayInputStream)39 BufferedInputStream (java.io.BufferedInputStream)28 OutputStream (java.io.OutputStream)26 Path (java.nio.file.Path)22 ArrayList (java.util.ArrayList)19 FileNotFoundException (java.io.FileNotFoundException)18 HashMap (java.util.HashMap)17 CRC32 (java.util.zip.CRC32)13