Example usage for java.util.zip ZipEntry setCompressedSize

List of usage examples for java.util.zip ZipEntry setCompressedSize

Introduction

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

Prototype

public void setCompressedSize(long csize) 

Source Link

Document

Sets the size of the compressed entry data.

Usage

From source file:com.smash.revolance.ui.model.helper.ArchiveHelper.java

public static File buildArchive(File archive, File... files) throws FileNotFoundException {
    FileOutputStream fos = new FileOutputStream(archive);
    ZipOutputStream zos = new ZipOutputStream(fos);
    int bytesRead;
    byte[] buffer = new byte[1024];
    CRC32 crc = new CRC32();

    for (File file : files) {
        if (!file.exists()) {
            System.err.println("Skipping: " + file);
            continue;
        }//from   w w  w.j ava  2  s  .  co  m
        BufferedInputStream bis = null;
        try {
            bis = new BufferedInputStream(new FileInputStream(file));
            crc.reset();
            while ((bytesRead = bis.read(buffer)) != -1) {
                crc.update(buffer, 0, bytesRead);
            }

            bis.close();

            // Reset to beginning of input stream
            bis = new BufferedInputStream(new FileInputStream(file));
            String entryPath = FileHelper.getRelativePath(archive.getParentFile(), file);

            ZipEntry entry = new ZipEntry(entryPath);
            entry.setMethod(ZipEntry.STORED);
            entry.setCompressedSize(file.length());
            entry.setSize(file.length());
            entry.setCrc(crc.getValue());
            zos.putNextEntry(entry);
            while ((bytesRead = bis.read(buffer)) != -1) {
                zos.write(buffer, 0, bytesRead);
            }
        } catch (FileNotFoundException e) {

        } catch (IOException e) {
            e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
        } finally {
            IOUtils.closeQuietly(bis);
        }
    }
    IOUtils.closeQuietly(zos);
    return archive;
}

From source file:com.diffplug.gradle.ZipMisc.java

/**
 * Modifies only the specified entries in a zip file. 
 *
 * @param input       a source from a zip file
 * @param output      an output to a zip file
 * @param toModify      a map from path to an input stream for the entries you'd like to change
 * @param toOmit      a set of entries you'd like to leave out of the zip
 * @throws IOException/*from  w  w w .  j a  va  2  s .  co  m*/
 */
public static void modify(ByteSource input, ByteSink output, Map<String, Function<byte[], byte[]>> toModify,
        Predicate<String> toOmit) throws IOException {
    try (ZipInputStream zipInput = new ZipInputStream(input.openBufferedStream());
            ZipOutputStream zipOutput = new ZipOutputStream(output.openBufferedStream())) {
        while (true) {
            // read the next entry
            ZipEntry entry = zipInput.getNextEntry();
            if (entry == null) {
                break;
            }

            Function<byte[], byte[]> replacement = toModify.get(entry.getName());
            if (replacement != null) {
                byte[] clean = ByteStreams.toByteArray(zipInput);
                byte[] modified = replacement.apply(clean);
                // if it's the entry being modified, enter the modified stuff
                try (InputStream replacementStream = new ByteArrayInputStream(modified)) {
                    ZipEntry newEntry = new ZipEntry(entry.getName());
                    newEntry.setComment(entry.getComment());
                    newEntry.setExtra(entry.getExtra());
                    newEntry.setMethod(entry.getMethod());
                    newEntry.setTime(entry.getTime());

                    zipOutput.putNextEntry(newEntry);
                    copy(replacementStream, zipOutput);
                }
            } else if (!toOmit.test(entry.getName())) {
                // if it isn't being modified, just copy the file stream straight-up
                ZipEntry newEntry = new ZipEntry(entry);
                newEntry.setCompressedSize(-1);
                zipOutput.putNextEntry(newEntry);
                copy(zipInput, zipOutput);
            }

            // close the entries
            zipInput.closeEntry();
            zipOutput.closeEntry();
        }
    }
}

From source file:net.librec.util.FileUtil.java

/**
 * Zip a given folder/*from  www .j  av  a2  s.c o m*/
 *
 * @param dirPath    a given folder: must be all files (not sub-folders)
 * @param filePath   zipped file
 * @throws Exception if error occurs
 */
public static void zipFolder(String dirPath, String filePath) throws Exception {
    File outFile = new File(filePath);
    ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(outFile));
    int bytesRead;
    byte[] buffer = new byte[1024];
    CRC32 crc = new CRC32();
    for (File file : listFiles(dirPath)) {
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
        crc.reset();
        while ((bytesRead = bis.read(buffer)) != -1) {
            crc.update(buffer, 0, bytesRead);
        }
        bis.close();

        // Reset to beginning of input stream
        bis = new BufferedInputStream(new FileInputStream(file));
        ZipEntry entry = new ZipEntry(file.getName());
        entry.setMethod(ZipEntry.STORED);
        entry.setCompressedSize(file.length());
        entry.setSize(file.length());
        entry.setCrc(crc.getValue());
        zos.putNextEntry(entry);
        while ((bytesRead = bis.read(buffer)) != -1) {
            zos.write(buffer, 0, bytesRead);
        }
        bis.close();
    }
    zos.close();

    LOG.debug("A zip-file is created to: " + outFile.getPath());
}

From source file:com.taobao.android.builder.tools.zip.ZipUtils.java

public static void addFileAndDirectoryToZip(File output, File srcDir, Map<String, ZipEntry> zipEntryMethodMap)
        throws Exception {
    if (output.isDirectory()) {
        throw new IOException("This is a directory!");
    }//from   w w  w .j  av  a 2  s  .co m
    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();
}

From source file:com.ibm.liberty.starter.ProjectZipConstructor.java

public void createZipFromMap(ZipOutputStream zos) throws IOException {
    System.out.println("Entering method ProjectZipConstructor.createZipFromMap()");
    Enumeration<String> en = fileMap.keys();
    while (en.hasMoreElements()) {
        String path = en.nextElement();
        byte[] byteArray = fileMap.get(path);
        ZipEntry entry = new ZipEntry(path);
        entry.setSize(byteArray.length);
        entry.setCompressedSize(-1);
        try {//from w  w  w . j a  va  2 s.  co  m
            zos.putNextEntry(entry);
            zos.write(byteArray);
        } catch (IOException e) {
            throw new IOException(e);
        }
    }
}

From source file:com.taobao.android.builder.tools.zip.ZipUtils.java

public static void rezip(File output, File srcDir, Map<String, ZipEntry> zipEntryMethodMap) throws Exception {
    if (output.isDirectory()) {
        throw new IOException("This is a directory!");
    }/*from   w  w w .  j  a v a2  s .c o m*/
    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(ze.getName());
            if (originEntry != null) {
                if (originEntry.getMethod() == STORED) {
                    ze.setCompressedSize(f.length());
                    InputStream in = new BufferedInputStream(new FileInputStream(f));
                    try {
                        CRC32 crc = new CRC32();
                        int c;
                        while ((c = in.read()) != -1) {
                            crc.update(c);
                        }
                        ze.setCrc(crc.getValue());
                    } finally {
                        in.close();
                    }
                }
                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();
}

From source file:com.yifanlu.PSXperiaTool.ZpakCreate.java

public void create(boolean noCompress) throws IOException {
    Logger.info("Generating zpak file from directory %s with compression = %b", mDirectory.getPath(),
            !noCompress);/*from w w  w  .  jav a2s  .co m*/
    IOFileFilter filter = new IOFileFilter() {
        public boolean accept(File file) {
            if (file.getName().startsWith(".")) {
                Logger.debug("Skipping file %s", file.getPath());
                return false;
            }
            return true;
        }

        public boolean accept(File file, String s) {
            if (s.startsWith(".")) {
                Logger.debug("Skipping file %s", file.getPath());
                return false;
            }
            return true;
        }
    };
    Iterator<File> it = FileUtils.iterateFiles(mDirectory, filter, TrueFileFilter.INSTANCE);
    ZipOutputStream out = new ZipOutputStream(mOut);
    out.setMethod(noCompress ? ZipEntry.STORED : ZipEntry.DEFLATED);
    while (it.hasNext()) {
        File current = it.next();
        FileInputStream in = new FileInputStream(current);
        ZipEntry zEntry = new ZipEntry(
                current.getPath().replace(mDirectory.getPath(), "").substring(1).replace("\\", "/"));
        if (noCompress) {
            zEntry.setSize(in.getChannel().size());
            zEntry.setCompressedSize(in.getChannel().size());
            zEntry.setCrc(getCRC32(current));
        }
        out.putNextEntry(zEntry);
        Logger.verbose("Adding file %s", current.getPath());
        int n;
        while ((n = in.read(mBuffer)) != -1) {
            out.write(mBuffer, 0, n);
        }
        in.close();
        out.closeEntry();
    }
    out.close();
    Logger.debug("Done with ZPAK creation.");
}

From source file:com.rover12421.shaka.apktool.lib.AndrolibAj.java

private void copyExistingFiles(ZipFile inputFile, ZipOutputStream outputFile, Map<String, String> excludeFiles)
        throws IOException {
    // First, copy the contents from the existing outFile:
    Enumeration<? extends ZipEntry> entries = inputFile.entries();
    while (entries.hasMoreElements()) {
        ZipEntry entry = new ZipEntry(entries.nextElement());
        if (excludeFiles.get(entry.getName()) != null) {
            //??.
            continue;
        }//www.ja  v a2 s  . c o m
        // We can't reuse the compressed size because it depends on compression sizes.
        entry.setCompressedSize(-1);
        outputFile.putNextEntry(entry);

        // No need to create directory entries in the final apk
        if (!entry.isDirectory()) {
            BrutIO.copy(inputFile, outputFile, entry);
        }

        outputFile.closeEntry();
    }
}

From source file:io.fabric8.maven.generator.springboot.SpringBootGenerator.java

private ZipEntry createZipEntry(File file, String fullPath) throws IOException {
    ZipEntry entry = new ZipEntry(fullPath);

    byte[] buffer = new byte[8192];
    int bytesRead = -1;
    try (InputStream is = new FileInputStream(file)) {
        CRC32 crc = new CRC32();
        int size = 0;
        while ((bytesRead = is.read(buffer)) != -1) {
            crc.update(buffer, 0, bytesRead);
            size += bytesRead;/*from  w  w w.j a  v  a  2  s.co  m*/
        }
        entry.setSize(size);
        entry.setCompressedSize(size);
        entry.setCrc(crc.getValue());
        entry.setMethod(ZipEntry.STORED);
        return entry;
    }
}

From source file:com.rover12421.shaka.apktool.lib.AndrolibAj.java

private void copyUnknownFiles(File appDir, ZipOutputStream outputFile, Map<String, String> files)
        throws IOException {
    String UNK_DIRNAME = getUNK_DIRNAME();
    File unknownFileDir = new File(appDir, UNK_DIRNAME);

    // loop through unknown files
    for (Map.Entry<String, String> unknownFileInfo : files.entrySet()) {
        File inputFile = new File(unknownFileDir, unknownFileInfo.getKey());
        if (inputFile.isDirectory()) {
            continue;
        }//from w w  w  .  ja v a  2 s  . co m

        ZipEntry newEntry = new ZipEntry(unknownFileInfo.getKey());
        int method = Integer.valueOf(unknownFileInfo.getValue());
        LogHelper.fine(
                String.format("Copying unknown file %s with method %d", unknownFileInfo.getKey(), method));
        if (method == ZipEntry.STORED) {
            newEntry.setMethod(ZipEntry.STORED);
            newEntry.setSize(inputFile.length());
            newEntry.setCompressedSize(-1);
            BufferedInputStream unknownFile = new BufferedInputStream(new FileInputStream(inputFile));
            CRC32 crc = calculateCrc(unknownFile);
            newEntry.setCrc(crc.getValue());

            //                LogHelper.getLogger().fine("\tsize: " + newEntry.getSize());
        } else {
            newEntry.setMethod(ZipEntry.DEFLATED);
        }
        outputFile.putNextEntry(newEntry);

        BrutIO.copy(inputFile, outputFile);
        outputFile.closeEntry();
    }
}