Example usage for java.util.zip ZipInputStream read

List of usage examples for java.util.zip ZipInputStream read

Introduction

In this page you can find the example usage for java.util.zip ZipInputStream read.

Prototype

public int read(byte b[]) throws IOException 

Source Link

Document

Reads up to b.length bytes of data from this input stream into an array of bytes.

Usage

From source file:org.geowebcache.sqlite.OperationsRest.java

private void extractFile(ZipInputStream inputStream, File outputFile) throws Exception {
    // extracting zip entry file content
    byte[] bytes = new byte[1024];
    try (FileOutputStream outputStream = new FileOutputStream(outputFile)) {
        int read;
        while ((read = inputStream.read(bytes)) != -1) {
            outputStream.write(bytes, 0, read);
        }//from ww  w  .ja  va  2  s  .  co m
    }
}

From source file:net.minecraftforge.fml.common.asm.transformers.AccessTransformer.java

private static void processJar(File inFile, File outFile, AccessTransformer[] transformers) throws IOException {
    ZipInputStream inJar = null;
    ZipOutputStream outJar = null;

    try {// w ww . j a  v a 2 s.c o  m
        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);
    }
}

From source file:com.jbrisbin.vpc.jobsched.batch.BatchMessageConverter.java

public Object fromMessage(Message message) throws MessageConversionException {
    MessageProperties props = message.getMessageProperties();
    if (isAuthorized(props) && props.getContentType().equals("application/zip")) {
        BatchMessage msg = new BatchMessage();
        msg.setId(new String(props.getCorrelationId()));
        String timeout = "2";
        if (props.getHeaders().containsKey("timeout")) {
            timeout = props.getHeaders().get("timeout").toString();
        }//from  w  w  w.  j  a v  a2  s  .  c o m
        msg.setTimeout(Integer.parseInt(timeout));

        ZipInputStream zin = new ZipInputStream(new ByteArrayInputStream(message.getBody()));
        ZipEntry zentry;
        try {
            while (null != (zentry = zin.getNextEntry())) {
                byte[] buff = new byte[4096];
                StringWriter json = new StringWriter();
                for (int bytesRead = 0; bytesRead > -1; bytesRead = zin.read(buff)) {
                    json.write(new String(buff, 0, bytesRead));
                }
                msg.getMessages().put(zentry.getName(), json.toString());
            }
        } catch (IOException e) {
            throw new MessageConversionException(e.getMessage(), e);
        }

        return msg;
    } else {
        throw new MessageConversionException("Invalid security key.");
    }
}

From source file:com.espringtran.compressor4j.processor.LzmaProcessor.java

/**
 * Read from compressed file// w  ww  .  j av a  2 s.c  om
 * 
 * @param srcPath
 *            path of compressed file
 * @param fileCompressor
 *            FileCompressor object
 * @throws Exception
 */
@Override
public void read(String srcPath, FileCompressor fileCompressor) throws Exception {
    long t1 = System.currentTimeMillis();
    byte[] data = FileUtil.convertFileToByte(srcPath);
    ByteArrayInputStream bais = new ByteArrayInputStream(data);
    LZMACompressorInputStream cis = new LZMACompressorInputStream(bais);
    ZipInputStream zis = new ZipInputStream(cis);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    try {
        byte[] buffer = new byte[1024];
        int readByte;
        ZipEntry entry = zis.getNextEntry();
        while (entry != null) {
            long t2 = System.currentTimeMillis();
            baos = new ByteArrayOutputStream();
            readByte = zis.read(buffer);
            while (readByte != -1) {
                baos.write(buffer, 0, readByte);
                readByte = zis.read(buffer);
            }
            zis.closeEntry();
            BinaryFile binaryFile = new BinaryFile(entry.getName(), baos.toByteArray());
            fileCompressor.addBinaryFile(binaryFile);
            LogUtil.createAddFileLog(fileCompressor, binaryFile, t2, System.currentTimeMillis());
            entry = zis.getNextEntry();
        }
    } catch (Exception e) {
        FileCompressor.LOGGER.error("Error on get compressor file", e);
    } finally {
        baos.close();
        zis.close();
        cis.close();
        bais.close();
    }
    LogUtil.createReadLog(fileCompressor, srcPath, data.length, t1, System.currentTimeMillis());
}

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

/**
 * zip/* ww w  .  jav  a  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.alcatel_lucent.nz.wnmsextract.reader.FileSelector.java

/**
 * The nested unzip method. Given a zip stream, decompress and store in file in temp_dir.
 * Replaced by unzip3./*  w  w  w . j av a2  s . com*/
 */
protected File unzip2(File zf) throws FileNotFoundException {
    //File f = null;
    String rename = zf.getAbsolutePath().replaceFirst("\\.zip", identifier + ".xml");//.replaceFirst("\\.gz", ".xml");
    File f = new File(rename);
    try {
        FileInputStream fis = new FileInputStream(zf);
        FileOutputStream fos = new FileOutputStream(rename);
        ZipInputStream zin = new ZipInputStream(fis);
        final byte[] content = new byte[BUFFER];
        int n = 0;
        while (-1 != (n = zin.read(content))) {
            fos.write(content, 0, n);
        }

        fos.flush();
        fos.close();

        fis.close();
        zin.close();
    } catch (IOException ioe) {
        jlog.error("Error processing Zip " + zf + " Excluding! :: " + ioe);
        return null;
    }
    //try again... what could go wrong
    if (checkMinFileSize(f) && retry_counter < MAX_UNGZIP_RETRIES) {
        retry_counter++;
        f.delete();
        f = unzip2(zf);
    }
    return f;
}

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

public void initializeMap() throws IOException {
    System.out.println("Entering method ProjectZipConstructor.initializeMap()");
    InputStream skeletonIS = this.getClass().getClassLoader().getResourceAsStream(SKELETON_JAR_FILENAME);
    ZipInputStream zis = new ZipInputStream(skeletonIS);
    ZipEntry ze;/*from  ww  w.j  a v a 2  s . c o m*/
    while ((ze = zis.getNextEntry()) != null) {
        String path = ze.getName();
        int length = 0;
        byte[] bytes = new byte[1024];
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        while ((length = zis.read(bytes)) != -1) {
            baos.write(bytes, 0, length);
        }
        putFileInMap(path, baos.toByteArray());
    }
    zis.close();
}

From source file:com.amalto.core.jobox.util.JoboxUtil.java

public static void extract(String zipPathFile, String destinationPath) throws Exception {
    FileInputStream fins = new FileInputStream(zipPathFile);
    ZipInputStream zipInputStream = new ZipInputStream(fins);
    try {//from   ww w.j  av a 2s  . c  o  m
        ZipEntry ze;
        byte ch[] = new byte[256];
        while ((ze = zipInputStream.getNextEntry()) != null) {
            File zipFile = new File(destinationPath + ze.getName());
            File zipFilePath = new File(zipFile.getParentFile().getPath());
            if (ze.isDirectory()) {
                if (!zipFile.exists()) {
                    if (!zipFile.mkdirs()) {
                        LOGGER.error("Create folder failed for '" + zipFile.getAbsolutePath() + "'."); //$NON-NLS-1$ //$NON-NLS-2$
                    }
                }
                zipInputStream.closeEntry();
            } else {
                if (!zipFilePath.exists()) {
                    if (!zipFilePath.mkdirs()) {
                        LOGGER.error("Create folder failed for '" + zipFilePath.getAbsolutePath() + "'."); //$NON-NLS-1$ //$NON-NLS-2$
                    }
                }
                FileOutputStream fileOutputStream = new FileOutputStream(zipFile);
                try {
                    int i;
                    while ((i = zipInputStream.read(ch)) != -1) {
                        fileOutputStream.write(ch, 0, i);
                    }
                    zipInputStream.closeEntry();
                } finally {
                    fileOutputStream.close();
                }
            }
        }
    } finally {
        IOUtils.closeQuietly(fins);
        IOUtils.closeQuietly(zipInputStream);
    }

}

From source file:com.coinblesk.client.backup.BackupRestoreDialogFragment.java

/**
 * Extracts the next file from the ZIP and returns the byte content.
 * @param zis Zip//w  w  w .  ja  v  a  2s. c  om
 * @return content
 * @throws IOException
 */
private byte[] extractFileFromZip(ZipInputStream zis) throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    byte[] buffer = new byte[1024];
    int count;
    while ((count = zis.read(buffer)) != -1) {
        baos.write(buffer, 0, count);
    }
    byte[] data = baos.toByteArray();
    return data;
}

From source file:com.espringtran.compressor4j.processor.Bzip2Processor.java

/**
 * Read from compressed file/*from  ww  w . ja  v  a2  s .c  o m*/
 * 
 * @param srcPath
 *            path of compressed file
 * @param fileCompressor
 *            FileCompressor object
 * @throws Exception
 */
@Override
public void read(String srcPath, FileCompressor fileCompressor) throws Exception {
    long t1 = System.currentTimeMillis();
    byte[] data = FileUtil.convertFileToByte(srcPath);
    ByteArrayInputStream bais = new ByteArrayInputStream(data);
    BZip2CompressorInputStream cis = new BZip2CompressorInputStream(bais);
    ZipInputStream zis = new ZipInputStream(cis);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    try {
        byte[] buffer = new byte[1024];
        int readByte;
        ZipEntry entry = zis.getNextEntry();
        while (entry != null) {
            long t2 = System.currentTimeMillis();
            baos = new ByteArrayOutputStream();
            readByte = zis.read(buffer);
            while (readByte != -1) {
                baos.write(buffer, 0, readByte);
                readByte = zis.read(buffer);
            }
            zis.closeEntry();
            BinaryFile binaryFile = new BinaryFile(entry.getName(), baos.toByteArray());
            fileCompressor.addBinaryFile(binaryFile);
            LogUtil.createAddFileLog(fileCompressor, binaryFile, t2, System.currentTimeMillis());
            entry = zis.getNextEntry();
        }
    } catch (Exception e) {
        FileCompressor.LOGGER.error("Error on get compressor file", e);
    } finally {
        baos.close();
        zis.close();
        cis.close();
        bais.close();
    }
    LogUtil.createReadLog(fileCompressor, srcPath, data.length, t1, System.currentTimeMillis());
}