Example usage for java.util.zip ZipEntry STORED

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

Introduction

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

Prototype

int STORED

To view the source code for java.util.zip ZipEntry STORED.

Click Source Link

Document

Compression method for uncompressed entries.

Usage

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  ww . j a 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();
    }
}

From source file:guru.benson.pinch.Pinch.java

/**
 * Download and inflate file from a ZIP stored on a HTTP server.
 *
 * @param entry/* w  w w. ja v a2  s.  co  m*/
 *     Entry representing file to download.
 * @param name
 *     Path where to store the downloaded file.
 * @param listener
 *
 * @throws IOException
 *     If an error occurred while reading from network or writing to disk.
 * @throws InterruptedException
 *     If the thread was interrupted.
 */
public void downloadFile(ExtendedZipEntry entry, String dir, String name, ProgressListener listener)
        throws IOException, InterruptedException {
    HttpURLConnection conn = null;
    InputStream is = null;
    FileOutputStream fos = null;

    try {
        File outFile = new File(dir != null ? dir + File.separator + name : name);

        if (!outFile.exists()) {
            if (outFile.getParentFile() != null) {
                outFile.getParentFile().mkdirs();
            }
        }

        // no need to download 0 byte size directories
        if (entry.isDirectory()) {
            return;
        }

        fos = new FileOutputStream(outFile);

        byte[] buf = new byte[2048];
        int read, bytes = 0;

        conn = getEntryInputStream(entry);

        // this is a stored (non-deflated) file, read it raw without inflating it
        if (entry.getMethod() == ZipEntry.STORED) {
            is = new BufferedInputStream(conn.getInputStream());
        } else {
            is = new InflaterInputStream(conn.getInputStream(), new Inflater(true));
        }

        long totalSize = entry.getSize();
        while ((read = is.read(buf)) != -1) {
            if (Thread.currentThread().isInterrupted()) {
                throw new InterruptedException("Download was interrupted");
            }
            // Ignore any extra data
            if (totalSize < read + bytes) {
                read = ((int) totalSize) - bytes;
            }

            fos.write(buf, 0, read);
            bytes += read;
            if (listener != null) {
                listener.onProgress(bytes, read, totalSize);
            }
        }

        log("Wrote " + bytes + " bytes to " + name);
    } finally {
        close(fos);
        close(is);
        disconnect(conn);
    }
}

From source file:com.facebook.buck.util.unarchive.UnzipTest.java

@Test
public void testStripsPrefixAndIgnoresSiblings() throws IOException {
    byte[] bazDotSh = "echo \"baz.sh\"\n".getBytes(Charsets.UTF_8);
    try (ZipArchiveOutputStream zip = new ZipArchiveOutputStream(zipFile.toFile())) {
        zip.putArchiveEntry(new ZipArchiveEntry("foo"));
        zip.closeArchiveEntry();/*  w w w .ja v  a 2  s. c o m*/
        zip.putArchiveEntry(new ZipArchiveEntry("foo/bar/baz.txt"));
        zip.write(DUMMY_FILE_CONTENTS, 0, DUMMY_FILE_CONTENTS.length);
        zip.closeArchiveEntry();

        ZipArchiveEntry exeEntry = new ZipArchiveEntry("foo/bar/baz.sh");
        exeEntry.setUnixMode(
                (int) MorePosixFilePermissions.toMode(PosixFilePermissions.fromString("r-x------")));
        exeEntry.setMethod(ZipEntry.STORED);
        exeEntry.setSize(bazDotSh.length);
        zip.putArchiveEntry(exeEntry);
        zip.write(bazDotSh);

        zip.closeArchiveEntry();
        zip.putArchiveEntry(new ZipArchiveEntry("sibling"));
        zip.closeArchiveEntry();
        zip.putArchiveEntry(new ZipArchiveEntry("sibling/some/dir/and/file.txt"));
        zip.write(DUMMY_FILE_CONTENTS, 0, DUMMY_FILE_CONTENTS.length);
        zip.closeArchiveEntry();
    }

    Path extractFolder = Paths.get("output_dir", "nested");

    ProjectFilesystem filesystem = TestProjectFilesystems.createProjectFilesystem(tmpFolder.getRoot());

    ArchiveFormat.ZIP.getUnarchiver().extractArchive(zipFile, filesystem, extractFolder,
            Optional.of(Paths.get("foo")), ExistingFileMode.OVERWRITE_AND_CLEAN_DIRECTORIES);

    assertFalse(filesystem.isDirectory(extractFolder.resolve("sibling")));
    assertFalse(filesystem.isDirectory(extractFolder.resolve("foo")));
    assertFalse(filesystem.isDirectory(extractFolder.resolve("some")));

    Path bazDotTxtPath = extractFolder.resolve("bar").resolve("baz.txt");
    Path bazDotShPath = extractFolder.resolve("bar").resolve("baz.sh");

    assertTrue(filesystem.isDirectory(extractFolder.resolve("bar")));
    assertTrue(filesystem.isFile(bazDotTxtPath));
    assertTrue(filesystem.isFile(bazDotShPath));
    assertTrue(filesystem.isExecutable(bazDotShPath));
    assertEquals(new String(bazDotSh), filesystem.readFileIfItExists(bazDotShPath).get());
    assertEquals(new String(DUMMY_FILE_CONTENTS), filesystem.readFileIfItExists(bazDotTxtPath).get());
}

From source file:com.jayway.maven.plugins.android.phase09package.ApkMojo.java

private void updateWithMetaInf(ZipOutputStream zos, File jarFile, Set<String> entries, boolean metaInfOnly)
        throws IOException {
    ZipFile zin = new ZipFile(jarFile);

    for (Enumeration<? extends ZipEntry> en = zin.entries(); en.hasMoreElements();) {
        ZipEntry ze = en.nextElement();

        if (ze.isDirectory()) {
            continue;
        }//from   w w w.j av a  2 s .c  om

        String zn = ze.getName();

        if (metaInfOnly) {
            if (!zn.startsWith("META-INF/")) {
                continue;
            }

            if (this.extractDuplicates && !entries.add(zn)) {
                continue;
            }

            if (!this.apkMetaInf.isIncluded(zn)) {
                continue;
            }
        }
        final ZipEntry ne;
        if (ze.getMethod() == ZipEntry.STORED) {
            ne = new ZipEntry(ze);
        } else {
            ne = new ZipEntry(zn);
        }

        zos.putNextEntry(ne);

        InputStream is = zin.getInputStream(ze);

        copyStreamWithoutClosing(is, zos);

        is.close();
        zos.closeEntry();
    }

    zin.close();
}

From source file:com.taobao.android.utils.ZipUtils.java

/**
 * zip//from w  w w  . j  a  va 2s  . c om
 *
 * @param zipFile
 * @param file
 * @param destPath  ?
 * @param overwrite ?
 * @throws IOException
 */
public static void addFileToZipFile(File zipFile, File outZipFile, File file, String destPath,
        boolean overwrite) throws IOException {
    byte[] buf = new byte[1024];
    ZipInputStream zin = new ZipInputStream(new FileInputStream(zipFile));
    ZipOutputStream out = new ZipOutputStream(new FileOutputStream(outZipFile));
    ZipEntry entry = zin.getNextEntry();
    boolean addFile = true;
    while (entry != null) {
        boolean addEntry = true;
        String name = entry.getName();
        if (StringUtils.equalsIgnoreCase(name, destPath)) {
            if (overwrite) {
                addEntry = false;
            } else {
                addFile = false;
            }
        }
        if (addEntry) {
            ZipEntry zipEntry = null;
            if (ZipEntry.STORED == entry.getMethod()) {
                zipEntry = new ZipEntry(entry);
            } else {
                zipEntry = new ZipEntry(name);
            }
            out.putNextEntry(zipEntry);
            int len;
            while ((len = zin.read(buf)) > 0) {
                out.write(buf, 0, len);
            }
        }
        entry = zin.getNextEntry();
    }

    if (addFile) {
        InputStream in = new FileInputStream(file);
        // Add ZIP entry to output stream.
        ZipEntry zipEntry = new ZipEntry(destPath);
        out.putNextEntry(zipEntry);
        // Transfer bytes from the file to the ZIP file
        int len;
        while ((len = in.read(buf)) > 0) {
            out.write(buf, 0, len);
        }
        // Complete the entry
        out.closeEntry();
        in.close();
    }
    // Close the streams
    zin.close();
    out.close();
}

From source file:com.simpligility.maven.plugins.android.phase09package.ApkMojo.java

private void updateWithMetaInf(ZipOutputStream zos, File jarFile, Set<String> entries, boolean metaInfOnly)
        throws IOException {
    ZipFile zin = new ZipFile(jarFile);

    for (Enumeration<? extends ZipEntry> en = zin.entries(); en.hasMoreElements();) {
        ZipEntry ze = en.nextElement();

        if (ze.isDirectory()) {
            continue;
        }//from w  w w  .  j  ava 2  s.co  m

        String zn = ze.getName();

        if (metaInfOnly) {
            if (!zn.startsWith("META-INF/")) {
                continue;
            }

            if (!this.apkMetaInf.isIncluded(zn)) {
                continue;
            }
        }

        boolean resourceTransformed = false;

        if (transformers != null) {
            for (ResourceTransformer transformer : transformers) {
                if (transformer.canTransformResource(zn)) {
                    getLog().info("Transforming " + zn + " using " + transformer.getClass().getName());
                    InputStream is = zin.getInputStream(ze);
                    transformer.processResource(zn, is, null);
                    is.close();
                    resourceTransformed = true;
                    break;
                }
            }
        }

        if (!resourceTransformed) {
            // Avoid duplicates that aren't accounted for by the resource transformers
            if (metaInfOnly && this.extractDuplicates && !entries.add(zn)) {
                continue;
            }

            InputStream is = zin.getInputStream(ze);

            final ZipEntry ne;
            if (ze.getMethod() == ZipEntry.STORED) {
                ne = new ZipEntry(ze);
            } else {
                ne = new ZipEntry(zn);
            }

            zos.putNextEntry(ne);

            copyStreamWithoutClosing(is, zos);

            is.close();
            zos.closeEntry();
        }
    }

    zin.close();
}

From source file:brut.androlib.Androlib.java

private void copyUnknownFiles(File appDir, ZipOutputStream outputFile, Map<String, String> files)
        throws IOException {
    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;
        }/*  w ww  .  ja  v a 2 s .  c  o  m*/

        ZipEntry newEntry = new ZipEntry(unknownFileInfo.getKey());
        int method = Integer.valueOf(unknownFileInfo.getValue());
        LOGGER.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 = BrutIO.calculateCrc(unknownFile);
            newEntry.setCrc(crc.getValue());
        } else {
            newEntry.setMethod(ZipEntry.DEFLATED);
        }
        outputFile.putNextEntry(newEntry);

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

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

/**
 * Zip a given folder/*from w  w  w .j  a  va 2s.  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:eu.europa.esig.dss.asic.signature.ASiCService.java

private ZipEntry getZipEntryMimeType(final byte[] mimeTypeBytes) {

    final ZipEntry entryMimetype = new ZipEntry(ZIP_ENTRY_MIMETYPE);
    entryMimetype.setMethod(ZipEntry.STORED);
    entryMimetype.setSize(mimeTypeBytes.length);
    entryMimetype.setCompressedSize(mimeTypeBytes.length);
    final CRC32 crc = new CRC32();
    crc.update(mimeTypeBytes);// w  ww  . j  av a2  s.  c  om
    entryMimetype.setCrc(crc.getValue());
    return entryMimetype;
}

From source file:UnpackedJarFile.java

public int getMethod() {
    return ZipEntry.STORED;
}