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.wso2.carbon.ei.migration.util.Utility.java
/** * Unzip the zip file/*from ww w .j ava2s . c o m*/ * * @param zipFile input zip file * @param outputFolder zip file output folder */ public static void unZipIt(String zipFile, String outputFolder) throws MigrationClientException { byte[] buffer = new byte[1024]; try { //create output directory is not exists File folder = new File(outputFolder); if (!folder.exists()) { folder.mkdir(); } //get the zip file content ZipInputStream zis = new ZipInputStream(new FileInputStream(zipFile)); //get the zipped file list entry ZipEntry ze = zis.getNextEntry(); while (ze != null) { String fileName = ze.getName(); if (fileName.toLowerCase().endsWith(".xml") || fileName.toLowerCase().endsWith(".wsdl") || fileName.toLowerCase().endsWith(".bpel") || fileName.endsWith("README")) { File newFile = new File(outputFolder + File.separator + fileName); //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(); } ze = zis.getNextEntry(); } zis.closeEntry(); zis.close(); } catch (IOException e) { throw new MigrationClientException(e.getMessage()); } }
From source file:de.pksoftware.springstrap.sapi.util.WebappZipper.java
/** * Unzip it/* w w w.j a v a 2 s.com*/ * @param zipFile input zip file * @param output zip file output folder */ public static void unzip(String zipFile, String outputFolder) { logger.info("Unzipping {} into {}.", zipFile, outputFolder); byte[] buffer = new byte[1024]; try { //get the zip file content ZipInputStream zis = new ZipInputStream(new FileInputStream(zipFile)); //get the zipped file list entry ZipEntry ze = zis.getNextEntry(); while (ze != null) { if (ze.isDirectory()) { ze = zis.getNextEntry(); continue; } String fileName = ze.getName(); File newFile = new File(outputFolder + File.separator + fileName); logger.debug("Unzipping: {}", newFile.getAbsoluteFile()); //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(); ze = zis.getNextEntry(); } zis.closeEntry(); zis.close(); logger.debug("Unzipping {} completed.", zipFile); } catch (IOException ex) { ex.printStackTrace(); } }
From source file:org.jahia.test.TestHelper.java
public static JahiaSite createSite(String name, String serverName, String templateSet, String prepackedZIPFile, String siteZIPName, String[] modulesToDeploy) throws Exception { modulesToDeploy = (modulesToDeploy == null) ? new String[0] : modulesToDeploy; JahiaUser admin = JahiaAdminUser.getAdminUser(null); JahiaSitesService service = ServicesRegistry.getInstance().getJahiaSitesService(); try {//from w w w. ja va 2 s . c o m JCRSessionWrapper session = JCRSessionFactory.getInstance().getCurrentSystemSession(null, null, null); JahiaSite existingSite = service.getSiteByKey(name, session); if (existingSite != null) { service.removeSite(existingSite); session.refresh(false); } } catch (RepositoryException ex) { logger.debug("Error while trying to remove the site", ex); } JahiaSite site = null; File siteZIPFile = null; File sharedZIPFile = null; try { if (!StringUtils.isEmpty(prepackedZIPFile)) { ZipInputStream zis = null; OutputStream os = null; try { zis = new ZipInputStream(new FileInputStream(new File(prepackedZIPFile))); ZipEntry z = null; while ((z = zis.getNextEntry()) != null) { if (siteZIPName.equalsIgnoreCase(z.getName()) || "users.zip".equals(z.getName())) { File zipFile = File.createTempFile("import", ".zip"); os = new FileOutputStream(zipFile); byte[] buf = new byte[4096]; int r; while ((r = zis.read(buf)) > 0) { os.write(buf, 0, r); } os.close(); if ("users.zip".equals(z.getName())) { sharedZIPFile = zipFile; } else { siteZIPFile = zipFile; } } } } catch (IOException e) { logger.error(e.getMessage(), e); } finally { if (os != null) { try { os.close(); } catch (IOException e) { logger.error(e.getMessage(), e); } } if (zis != null) { try { zis.close(); } catch (IOException e) { logger.error(e.getMessage(), e); } } } } if (sharedZIPFile != null) { try { ImportExportBaseService.getInstance().importSiteZip( sharedZIPFile != null ? new FileSystemResource(sharedZIPFile) : null, null, null); } catch (RepositoryException e) { logger.warn("shared.zip could not be imported", e); } } site = service.addSite(admin, name, serverName, name, name, SettingsBean.getInstance().getDefaultLocale(), templateSet, modulesToDeploy, siteZIPFile == null ? "noImport" : "fileImport", siteZIPFile != null ? new FileSystemResource(siteZIPFile) : null, null, false, false, null); site = service.getSiteByKey(name); } finally { if (sharedZIPFile != null) { sharedZIPFile.delete(); } if (siteZIPFile != null) { siteZIPFile.delete(); } } return site; }
From source file:org.sakaiproject.portal.charon.test.PortalTestFileUtils.java
/** * unpack a segment from a zip/*w w w .j a va 2s . c om*/ * * @param addsi * @param packetStream * @param version */ public static void unpack(InputStream source, File destination) throws IOException { ZipInputStream zin = new ZipInputStream(source); ZipEntry zipEntry = null; FileOutputStream fout = null; try { byte[] buffer = new byte[4096]; while ((zipEntry = zin.getNextEntry()) != null) { long ts = zipEntry.getTime(); // the zip entry needs to be a full path from the // searchIndexDirectory... hence this is correct File f = new File(destination, zipEntry.getName()); if (log.isDebugEnabled()) log.debug(" Unpack " + f.getAbsolutePath()); f.getParentFile().mkdirs(); fout = new FileOutputStream(f); int len; while ((len = zin.read(buffer)) > 0) { fout.write(buffer, 0, len); } zin.closeEntry(); fout.close(); f.setLastModified(ts); } } finally { try { fout.close(); } catch (Exception ex) { } } }
From source file:prototypes.ws.proxy.soap.commons.io.Files.java
/** * * @param zipFile//from w w w. j a v a2 s.c o m * @return */ public static String unzipFile(String zipFile) { FileOutputStream fos = null; try { File folder = createTempDirectory(); byte[] buffer = new byte[1024]; // create output directory is not exists if (!folder.exists()) { folder.mkdir(); } // get the zip file content ZipInputStream zis = new ZipInputStream(new FileInputStream(zipFile)); // get the zipped file list entry ZipEntry ze = zis.getNextEntry(); while (ze != null) { String fileName = ze.getName(); File newFile = new File(folder.getAbsolutePath() + File.separator + fileName); LOGGER.debug("file unzip : {}", newFile.getAbsoluteFile()); // create all non exists folders // else you will hit FileNotFoundException for compressed folder new File(newFile.getParent()).mkdirs(); fos = new FileOutputStream(newFile); int len; while ((len = zis.read(buffer)) > 0) { fos.write(buffer, 0, len); } fos.close(); ze = zis.getNextEntry(); } zis.closeEntry(); zis.close(); return folder.getAbsolutePath(); } catch (IOException ex) { LOGGER.error(ex.getMessage(), ex); } finally { if (fos != null) { try { fos.close(); } catch (IOException ex) { LOGGER.warn(Messages.MSG_ERROR_DETAILS, ex); } } } return null; }
From source file:Main.java
public static boolean unzipFile(InputStream fis, String destDir) { final byte[] buffer = new byte[4096]; ZipInputStream zis = null; Log.e("Unzip", "destDir = " + destDir); try {/* ww w. j a va 2s. com*/ // make sure the directory is existent File dstFile = new File(destDir); if (!dstFile.exists()) { dstFile.mkdirs(); } else { int fileLenght = dstFile.listFiles().length; if (fileLenght >= 2) { return true; } } zis = new ZipInputStream(fis); ZipEntry entry; while ((entry = zis.getNextEntry()) != null) { String fileName = entry.getName(); if (entry.isDirectory()) { new File(destDir, fileName).mkdirs(); } else { BufferedOutputStream bos = new BufferedOutputStream( new FileOutputStream(new File(destDir, fileName))); int lenRead; while ((lenRead = zis.read(buffer)) != -1) { bos.write(buffer, 0, lenRead); } bos.close(); } zis.closeEntry(); } return true; } catch (IOException e) { e.printStackTrace(); } finally { if (zis != null) { try { zis.close(); } catch (IOException e) { e.printStackTrace(); } } } return false; }
From source file:com.frostwire.util.ZipUtils.java
private static void unzipEntries(File folder, ZipInputStream zis, int itemCount, long time, ZipListener listener) throws IOException, FileNotFoundException { ZipEntry ze = null;/* w w w . j a v a 2s . co m*/ int item = 0; while ((ze = zis.getNextEntry()) != null) { item++; String fileName = ze.getName(); File newFile = new File(folder, fileName); LOG.debug("unzip: " + newFile.getAbsoluteFile()); if (ze.isDirectory()) { if (!newFile.mkdirs()) { break; } continue; } if (listener != null) { int progress = (item == itemCount) ? 100 : (int) (((double) (item * 100)) / (double) (itemCount)); listener.onUnzipping(fileName, progress); } FileOutputStream fos = new FileOutputStream(newFile); try { int n; byte[] buffer = new byte[1024]; while ((n = zis.read(buffer)) > 0) { fos.write(buffer, 0, n); if (listener != null && listener.isCanceled()) { // not the best way throw new IOException("Uncompress operation cancelled"); } } } finally { fos.close(); zis.closeEntry(); } newFile.setLastModified(time); } }
From source file:utils.APIImporter.java
/** * function to unzip the imported folder * @param zipFile zip file path/*from ww w . j a v a 2 s.co m*/ * @param outputFolder folder to copy the zip content * @throws APIImportException */ private static void unzipFolder(String zipFile, String outputFolder) throws APIImportException { byte[] buffer = new byte[1024]; try { ZipInputStream zis = new ZipInputStream(new FileInputStream(zipFile)); //get the zipped file list entry ZipEntry ze = zis.getNextEntry(); FileOutputStream fos = null; while (ze != null) { String fileName = ze.getName(); File newFile = new File(outputFolder + File.separator + fileName); //create all non exists folders //else you will hit FileNotFoundException for compressed folder new File(newFile.getParent()).mkdirs(); fos = new FileOutputStream(newFile); int len; while ((len = zis.read(buffer)) > 0) { fos.write(buffer, 0, len); } fos.close(); ze = zis.getNextEntry(); } zis.closeEntry(); IOUtils.closeQuietly(zis); IOUtils.closeQuietly(fos); // ZipInputStream zis = new ZipInputStream(new FileInputStream(zipFile)); // //get the zipped file list entry // ZipEntry ze = zis.getNextEntry(); // FileOutputStream fos = null; // while(ze!=null){ // String fileName = ze.getName(); // File newFile = new File(outputFolder + File.separator + fileName); // //create all non exists folders // //else it hit FileNotFoundException for compressed folder // boolean directoryStatus = new File(newFile.getParent()).mkdirs(); // while (directoryStatus){ // fos = new FileOutputStream(newFile); // int len; // while ((len = zis.read(buffer)) > 0) { // fos.write(buffer, 0, len); // } // ze = zis.getNextEntry(); // } // } // zis.closeEntry(); // IOUtils.closeQuietly(fos); // IOUtils.closeQuietly(zis); } catch (IOException e) { String errorMsg = "Cannot extract the zip entries "; log.error(errorMsg, e); throw new APIImportException(errorMsg, e); } }
From source file:com.matze5800.paupdater.Functions.java
public static void addFilesToExistingZip(File zipFile, File[] files) throws IOException { File tempFile = new File(Environment.getExternalStorageDirectory() + "/pa_updater", "temp_kernel.zip"); tempFile.delete();/*from ww w. j ava 2s. com*/ boolean renameOk = zipFile.renameTo(tempFile); if (!renameOk) { throw new RuntimeException( "could not rename the file " + zipFile.getAbsolutePath() + " to " + tempFile.getAbsolutePath()); } byte[] buf = new byte[1024]; ZipInputStream zin = new ZipInputStream(new FileInputStream(tempFile)); ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipFile)); ZipEntry entry = zin.getNextEntry(); while (entry != null) { String name = entry.getName(); boolean notInFiles = true; for (File f : files) { if (f.getName().equals(name)) { notInFiles = false; break; } } if (notInFiles) { out.putNextEntry(new ZipEntry(name)); int len; while ((len = zin.read(buf)) > 0) { out.write(buf, 0, len); } } entry = zin.getNextEntry(); } zin.close(); for (int i = 0; i < files.length; i++) { InputStream in = new FileInputStream(files[i]); out.putNextEntry(new ZipEntry(files[i].getName())); int len; while ((len = in.read(buf)) > 0) { out.write(buf, 0, len); } out.closeEntry(); in.close(); } out.close(); tempFile.delete(); }
From source file:org.datavec.api.util.ArchiveUtils.java
/** * Extracts files to the specified destination * @param file the file to extract to/* w ww .ja v a 2s . c om*/ * @param dest the destination directory * @throws java.io.IOException */ public static void unzipFileTo(String file, String dest) throws IOException { File target = new File(file); if (!target.exists()) throw new IllegalArgumentException("Archive doesnt exist"); FileInputStream fin = new FileInputStream(target); int BUFFER = 2048; byte data[] = new byte[BUFFER]; if (file.endsWith(".zip") || file.endsWith(".jar")) { //getFromOrigin the zip file content ZipInputStream zis = new ZipInputStream(fin); //getFromOrigin the zipped file list entry ZipEntry ze = zis.getNextEntry(); while (ze != null) { String fileName = ze.getName(); File newFile = new File(dest + File.separator + fileName); if (ze.isDirectory()) { newFile.mkdirs(); zis.closeEntry(); ze = zis.getNextEntry(); continue; } log.info("file unzip : " + newFile.getAbsoluteFile()); //create all non exists folders //else you will hit FileNotFoundException for compressed folder FileOutputStream fos = new FileOutputStream(newFile); int len; while ((len = zis.read(data)) > 0) { fos.write(data, 0, len); } fos.flush(); fos.close(); zis.closeEntry(); ze = zis.getNextEntry(); } zis.close(); } else if (file.endsWith(".tar")) { BufferedInputStream in = new BufferedInputStream(fin); TarArchiveInputStream tarIn = new TarArchiveInputStream(in); TarArchiveEntry entry = null; /** Read the tar entries using the getNextEntry method **/ while ((entry = (TarArchiveEntry) tarIn.getNextEntry()) != null) { log.info("Extracting: " + entry.getName()); /** If the entry is a directory, createComplex the directory. **/ if (entry.isDirectory()) { File f = new File(dest + File.separator + entry.getName()); f.mkdirs(); } /** * If the entry is a file,write the decompressed file to the disk * and close destination stream. **/ else { int count; FileOutputStream fos = new FileOutputStream(dest + File.separator + entry.getName()); BufferedOutputStream destStream = new BufferedOutputStream(fos, BUFFER); while ((count = tarIn.read(data, 0, BUFFER)) != -1) { destStream.write(data, 0, count); } destStream.flush(); ; IOUtils.closeQuietly(destStream); } } /** Close the input stream **/ tarIn.close(); } else if (file.endsWith(".tar.gz") || file.endsWith(".tgz")) { BufferedInputStream in = new BufferedInputStream(fin); GzipCompressorInputStream gzIn = new GzipCompressorInputStream(in); TarArchiveInputStream tarIn = new TarArchiveInputStream(gzIn); TarArchiveEntry entry = null; /** Read the tar entries using the getNextEntry method **/ while ((entry = (TarArchiveEntry) tarIn.getNextEntry()) != null) { log.info("Extracting: " + entry.getName()); /** If the entry is a directory, createComplex the directory. **/ if (entry.isDirectory()) { File f = new File(dest + File.separator + entry.getName()); f.mkdirs(); } /** * If the entry is a file,write the decompressed file to the disk * and close destination stream. **/ else { int count; FileOutputStream fos = new FileOutputStream(dest + File.separator + entry.getName()); BufferedOutputStream destStream = new BufferedOutputStream(fos, BUFFER); while ((count = tarIn.read(data, 0, BUFFER)) != -1) { destStream.write(data, 0, count); } destStream.flush(); IOUtils.closeQuietly(destStream); } } /** Close the input stream **/ tarIn.close(); } else if (file.endsWith(".gz")) { GZIPInputStream is2 = new GZIPInputStream(fin); File extracted = new File(target.getParent(), target.getName().replace(".gz", "")); if (extracted.exists()) extracted.delete(); extracted.createNewFile(); OutputStream fos = FileUtils.openOutputStream(extracted); IOUtils.copyLarge(is2, fos); is2.close(); fos.flush(); fos.close(); } }