List of usage examples for java.util.zip ZipInputStream read
public int read(byte b[]) throws IOException
b.length
bytes of data from this input stream into an array of bytes. From source file:org.freebxml.omar.common.Utility.java
/** * * Extracts Zip file contents relative to baseDir * @return An ArrayList containing the File instances for each unzipped file *//*from w w w . j a v a 2 s.c o m*/ public static ArrayList unZip(String baseDir, InputStream is) throws IOException { ArrayList files = new ArrayList(); ZipInputStream zis = new ZipInputStream(is); while (true) { // Get the next zip entry. Break out of the loop if there are // no more. ZipEntry zipEntry = zis.getNextEntry(); if (zipEntry == null) break; String entryName = zipEntry.getName(); if (FILE_SEPARATOR.equalsIgnoreCase("\\")) { // Convert '/' to Windows file separator entryName = entryName.replaceAll("/", "\\\\"); } String fileName = baseDir + FILE_SEPARATOR + entryName; //Make sure that directory exists. String dirName = fileName.substring(0, fileName.lastIndexOf(FILE_SEPARATOR)); File dir = new File(dirName); dir.mkdirs(); //Entry could be a directory if (!(zipEntry.isDirectory())) { //Entry is a file not a directory. //Write out the content of of entry to file File file = new File(fileName); files.add(file); FileOutputStream fos = new FileOutputStream(file); // Read data from the zip entry. The read() method will return // -1 when there is no more data to read. byte[] buffer = new byte[1000]; int n; while ((n = zis.read(buffer)) > -1) { // In real life, you'd probably write the data to a file. fos.write(buffer, 0, n); } zis.closeEntry(); fos.close(); } else { zis.closeEntry(); } } zis.close(); return files; }
From source file:org.pentaho.di.job.entries.hadooptransjobexecutor.DistributedCacheUtil.java
/** * Extract a zip archive to a directory. * * @param archive Zip archive to extract * @param dest Destination directory. This must not exist! * @return Directory the zip was extracted into * @throws IllegalArgumentException when the archive file does not exist or the destination directory already exists * @throws IOException/* w w w . ja v a 2 s . c o m*/ * @throws KettleFileException */ public FileObject extract(FileObject archive, FileObject dest) throws IOException, KettleFileException { if (!archive.exists()) { throw new IllegalArgumentException("archive does not exist: " + archive.getURL().getPath()); } if (dest.exists()) { throw new IllegalArgumentException("destination already exists"); } dest.createFolder(); try { byte[] buffer = new byte[DEFAULT_BUFFER_SIZE]; int len = 0; ZipInputStream zis = new ZipInputStream(archive.getContent().getInputStream()); try { ZipEntry ze; while ((ze = zis.getNextEntry()) != null) { FileObject entry = KettleVFS.getFileObject(dest + Const.FILE_SEPARATOR + ze.getName()); if (ze.isDirectory()) { entry.createFolder(); continue; } OutputStream os = KettleVFS.getOutputStream(entry, false); try { while ((len = zis.read(buffer)) > 0) { os.write(buffer, 0, len); } } finally { if (os != null) { os.close(); } } } } finally { if (zis != null) { zis.close(); } } } catch (Exception ex) { // Try to clean up the temp directory and all files if (!deleteDirectory(dest)) { throw new KettleFileException("Could not clean up temp dir after error extracting", ex); } throw new KettleFileException("error extracting archive", ex); } return dest; }
From source file:org.jboss.dashboard.ui.resources.GraphicElement.java
/** * Unzip given stream to destination file. * * @param zin Stream to unzip.//from ww w . j a v a 2s. co m * @param s File name for destination. * @throws java.io.IOException in case there are errors in the stream. */ protected void unzip(ZipInputStream zin, String s, String resourcePath) throws IOException { File f = new File(s); File parent = new File(f.getParent()); parent.mkdirs(); FileOutputStream out = null; try { out = new FileOutputStream(s); out.write(getPrefixForResourcePath(resourcePath).getBytes()); byte[] b = new byte[1024]; int len; while ((len = zin.read(b)) != -1) { out.write(b, 0, len); } out.write(getSuffixForResourcePath(resourcePath).getBytes()); } finally { if (out != null) out.close(); } }
From source file:org.chromium.APKPackager.java
private void extractToFolder(File zipfile, File tempdir) { InputStream inputStream = null; try {//from www . j a v a 2 s . c o m FileInputStream zipStream = new FileInputStream(zipfile); inputStream = new BufferedInputStream(zipStream); ZipInputStream zis = new ZipInputStream(inputStream); inputStream = zis; ZipEntry ze; byte[] buffer = new byte[32 * 1024]; while ((ze = zis.getNextEntry()) != null) { String compressedName = ze.getName(); if (ze.isDirectory()) { File dir = new File(tempdir, compressedName); dir.mkdirs(); } else { File file = new File(tempdir, compressedName); file.getParentFile().mkdirs(); if (file.exists() || file.createNewFile()) { FileOutputStream fout = new FileOutputStream(file); int count; while ((count = zis.read(buffer)) != -1) { fout.write(buffer, 0, count); } fout.close(); } } zis.closeEntry(); } } catch (Exception e) { Log.e(LOG_TAG, "Unzip error ", e); } finally { if (inputStream != null) { try { inputStream.close(); } catch (IOException e) { } } } }
From source file:org.wso2.carbon.apimgt.hostobjects.TenantManagerHostObject.java
private static void deployTenantTheme(FileHostObject themeFile, String tenant) throws APIManagementException { ZipInputStream zis = null; byte[] buffer = new byte[1024]; String outputFolder = TenantManagerHostObject.getStoreTenantThemesPath() + tenant; InputStream zipInputStream = null; try {// w w w.j a v a2 s .c om zipInputStream = themeFile.getInputStream(); } catch (ScriptException e) { handleException("Error occurred while deploying tenant theme file", e); } try { //create output directory if it is not exists File folder = new File(outputFolder); if (!folder.exists()) { if (!folder.mkdirs()) { handleException("Unable to create tenant theme directory"); } } //get the zip file content zis = new ZipInputStream(zipInputStream); //get the zipped file list entry ZipEntry ze = zis.getNextEntry(); String ext = null; while (ze != null) { String fileName = ze.getName(); File newFile = new File(outputFolder + File.separator + fileName); if (ze.isDirectory()) { if (!newFile.exists()) { boolean status = newFile.mkdir(); if (status) { //todo handle exception } } } else { ext = FilenameUtils.getExtension(ze.getName()); if (TenantManagerHostObject.EXTENTION_WHITELIST.contains(ext)) { //create all non exists folders //else you will hit FileNotFoundException for compressed folder new File(newFile.getParent()).mkdirs(); FileOutputStream fos = new FileOutputStream(newFile); int len; while ((len = zis.read(buffer)) > 0) { fos.write(buffer, 0, len); } fos.close(); } else { log.warn("Unsupported file is uploaded with tenant theme by " + tenant + " : file name : " + ze.getName()); } } ze = zis.getNextEntry(); } zis.closeEntry(); zis.close(); } catch (IOException ex) { handleException("Failed to deploy tenant theme", ex); //todo remove if the tenant theme directory is created. } finally { IOUtils.closeQuietly(zis); IOUtils.closeQuietly(zipInputStream); } }
From source file:it.cnr.icar.eric.common.Utility.java
/** * * Extracts Zip file contents relative to baseDir * @return An ArrayList containing the File instances for each unzipped file *///from w ww . j av a2 s . co m public static ArrayList<File> unZip(String baseDir, InputStream is) throws IOException { ArrayList<File> files = new ArrayList<File>(); ZipInputStream zis = new ZipInputStream(is); while (true) { // Get the next zip entry. Break out of the loop if there are // no more. ZipEntry zipEntry = zis.getNextEntry(); if (zipEntry == null) break; String entryName = zipEntry.getName(); if (FILE_SEPARATOR.equalsIgnoreCase("\\")) { // Convert '/' to Windows file separator entryName = entryName.replaceAll("/", "\\\\"); } String fileName = baseDir + FILE_SEPARATOR + entryName; //Make sure that directory exists. String dirName = fileName.substring(0, fileName.lastIndexOf(FILE_SEPARATOR)); File dir = new File(dirName); dir.mkdirs(); //Entry could be a directory if (!(zipEntry.isDirectory())) { //Entry is a file not a directory. //Write out the content of of entry to file File file = new File(fileName); files.add(file); FileOutputStream fos = new FileOutputStream(file); // Read data from the zip entry. The read() method will return // -1 when there is no more data to read. byte[] buffer = new byte[1000]; int n; while ((n = zis.read(buffer)) > -1) { // In real life, you'd probably write the data to a file. fos.write(buffer, 0, n); } zis.closeEntry(); fos.close(); } else { zis.closeEntry(); } } zis.close(); return files; }
From source file:org.opennms.upgrade.api.AbstractOnmsUpgrade.java
/** * UNZIP a file./*from w ww.ja v a 2 s.c o m*/ * * @param zipFile the input ZIP file * @param outputFolder the output folder * @throws OnmsUpgradeException the OpenNMS upgrade exception */ protected void unzipFile(File zipFile, File outputFolder) throws OnmsUpgradeException { try { if (!outputFolder.exists()) { if (!outputFolder.mkdirs()) { LOG.warn("Could not make directory: {}", outputFolder.getPath()); } } FileInputStream fis; byte[] buffer = new byte[1024]; fis = new FileInputStream(zipFile); ZipInputStream zis = new ZipInputStream(fis); ZipEntry ze = zis.getNextEntry(); while (ze != null) { String fileName = ze.getName(); File newFile = new File(outputFolder, fileName); log(" Unzipping to %s\n", newFile.getAbsolutePath()); new File(newFile.getParent()).mkdirs(); FileOutputStream fos = new FileOutputStream(newFile); int len; while ((len = zis.read(buffer)) > 0) { fos.write(buffer, 0, len); } fos.close(); zis.closeEntry(); ze = zis.getNextEntry(); } zis.closeEntry(); zis.close(); fis.close(); } catch (Exception e) { throw new OnmsUpgradeException("Cannot UNZIP file because " + e.getMessage(), e); } }
From source file:edu.harvard.mcz.dwcaextractor.DwCaExtractor.java
/** * Setup conditions to run.//from w ww . j a v a2s . c om * * @param args command line arguments * @return true if setup was successful, false otherwise. */ protected boolean setup(String[] args) { boolean setupOK = false; CmdLineParser parser = new CmdLineParser(this); //parser.setUsageWidth(4096); try { parser.parseArgument(args); if (help) { parser.printUsage(System.out); System.exit(0); } if (archiveFilePath != null) { String filePath = archiveFilePath; logger.debug(filePath); File file = new File(filePath); if (!file.exists()) { // Error logger.error(filePath + " not found."); } if (!file.canRead()) { // error logger.error("Unable to read " + filePath); } if (file.isDirectory()) { // check if it is an unzipped dwc archive. dwcArchive = openArchive(file); } if (file.isFile()) { // unzip it File outputDirectory = new File(file.getName().replace(".", "_") + "_content"); if (!outputDirectory.exists()) { outputDirectory.mkdir(); try { byte[] buffer = new byte[1024]; ZipInputStream inzip = new ZipInputStream(new FileInputStream(file)); ZipEntry entry = inzip.getNextEntry(); while (entry != null) { String fileName = entry.getName(); File expandedFile = new File(outputDirectory.getPath() + File.separator + fileName); new File(expandedFile.getParent()).mkdirs(); FileOutputStream expandedfileOutputStream = new FileOutputStream(expandedFile); int len; while ((len = inzip.read(buffer)) > 0) { expandedfileOutputStream.write(buffer, 0, len); } expandedfileOutputStream.close(); entry = inzip.getNextEntry(); } inzip.closeEntry(); inzip.close(); logger.debug("Unzipped archive into " + outputDirectory.getPath()); } catch (FileNotFoundException e) { logger.error(e.getMessage()); } catch (IOException e) { logger.error(e.getMessage(), e); } } // look into the unzipped directory dwcArchive = openArchive(outputDirectory); } if (dwcArchive != null) { if (checkArchive()) { // Check output csvPrinter = new CSVPrinter(new FileWriter(outputFilename, append), CSVFormat.DEFAULT.withQuoteMode(QuoteMode.NON_NUMERIC)); // no exception thrown setupOK = true; } } else { System.out.println("Problem opening archive."); logger.error("Unable to unpack archive file."); } logger.debug(setupOK); } } catch (CmdLineException e) { logger.error(e.getMessage()); parser.printUsage(System.err); } catch (IOException e) { logger.error(e.getMessage()); System.out.println(e.getMessage()); parser.printUsage(System.err); } return setupOK; }
From source file:se.bitcraze.crazyflielib.bootloader.Bootloader.java
public void unzip(File zipFile) { mLogger.debug("Trying to unzip " + zipFile + "..."); InputStream fis = null;/*from w w w . j av a 2 s . c o m*/ ZipInputStream zis = null; FileOutputStream fos = null; String parent = zipFile.getAbsoluteFile().getParent(); try { fis = new FileInputStream(zipFile); zis = new ZipInputStream(new BufferedInputStream(fis)); ZipEntry ze; while ((ze = zis.getNextEntry()) != null) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); byte[] buffer = new byte[1024]; int count; while ((count = zis.read(buffer)) != -1) { baos.write(buffer, 0, count); } String filename = ze.getName(); byte[] bytes = baos.toByteArray(); // write files File filePath = new File(parent + "/" + getFileNameWithoutExtension(zipFile) + "/" + filename); // create subdir filePath.getParentFile().mkdirs(); fos = new FileOutputStream(filePath); fos.write(bytes); //check if (filePath.exists() && filePath.length() > 0) { mLogger.debug(filename + " successfully extracted to " + filePath.getAbsolutePath()); } else { mLogger.error(filename + " was not extracted."); } } } catch (FileNotFoundException ffe) { mLogger.error(ffe.getMessage()); } catch (IOException ioe) { mLogger.error(ioe.getMessage()); } finally { if (zis != null) { try { zis.close(); } catch (IOException e) { mLogger.error(e.getMessage()); } } if (fos != null) { try { fos.close(); } catch (IOException e) { mLogger.error(e.getMessage()); } } } }
From source file:org.dhatim.archive.Archive.java
/** * Add the entries from the supplied {@link ZipInputStream} to this archive instance. * @param zipStream The zip stream./*w w w. ja va2 s . c o m*/ * @return This archive instance. * @throws IOException Error reading zip stream. */ public Archive addEntries(ZipInputStream zipStream) throws IOException { AssertArgument.isNotNull(zipStream, "zipStream"); try { ZipEntry zipEntry = zipStream.getNextEntry(); ByteArrayOutputStream outByteStream = new ByteArrayOutputStream(); byte[] byteReadBuffer = new byte[512]; int byteReadCount; while (zipEntry != null) { if (zipEntry.isDirectory()) { addEntry(zipEntry.getName(), (byte[]) null); } else { while ((byteReadCount = zipStream.read(byteReadBuffer)) != -1) { outByteStream.write(byteReadBuffer, 0, byteReadCount); } addEntry(zipEntry.getName(), outByteStream.toByteArray()); outByteStream.reset(); } zipEntry = zipStream.getNextEntry(); } } finally { try { zipStream.close(); } catch (IOException e) { logger.debug("Unexpected error closing EDI Mapping Model Zip stream.", e); } } return this; }