List of usage examples for java.util.zip ZipInputStream getNextEntry
public ZipEntry getNextEntry() throws IOException
From source file:com.taobao.android.tpatch.utils.JarSplitUtils.java
/** * 2jar?,??jar//from w w w . j a v a 2s. c o m * @param baseJar * @param diffJar * @param outJar */ public static void unionJar(File baseJar, File diffJar, File outJar) throws IOException { File outParentFolder = outJar.getParentFile(); if (!outParentFolder.exists()) { outParentFolder.mkdirs(); } List<String> diffEntries = getZipEntries(diffJar); FileOutputStream fos = new FileOutputStream(outJar); ZipOutputStream jos = new ZipOutputStream(fos); final byte[] buffer = new byte[8192]; FileInputStream fis = new FileInputStream(baseJar); ZipInputStream zis = new ZipInputStream(fis); try { // loop on the entries of the jar file package and put them in the final jar ZipEntry entry; while ((entry = zis.getNextEntry()) != null) { // do not take directories or anything inside a potential META-INF folder. if (entry.isDirectory()) { continue; } String name = entry.getName(); if (diffEntries.contains(name)) { continue; } JarEntry newEntry; // Preserve the STORED method of the input entry. if (entry.getMethod() == JarEntry.STORED) { newEntry = new JarEntry(entry); } else { // Create a new entry so that the compressed len is recomputed. newEntry = new JarEntry(name); } // add the entry to the jar archive jos.putNextEntry(newEntry); // read the content of the entry from the input stream, and write it into the archive. int count; while ((count = zis.read(buffer)) != -1) { jos.write(buffer, 0, count); } // close the entries for this file jos.closeEntry(); zis.closeEntry(); } } finally { zis.close(); } fis.close(); jos.close(); }
From source file:com.taobao.android.tpatch.utils.JarSplitUtils.java
/** * jarclass// w ww . j av a 2 s. co m * @param inJar * @param removeClasses */ public static void removeFilesFromJar(File inJar, List<String> removeClasses) throws IOException { if (null == removeClasses || removeClasses.isEmpty()) { return; } File outJar = new File(inJar.getParentFile(), inJar.getName() + ".tmp"); File outParentFolder = outJar.getParentFile(); if (!outParentFolder.exists()) { outParentFolder.mkdirs(); } FileOutputStream fos = new FileOutputStream(outJar); ZipOutputStream jos = new ZipOutputStream(fos); final byte[] buffer = new byte[8192]; FileInputStream fis = new FileInputStream(inJar); ZipInputStream zis = new ZipInputStream(fis); try { // loop on the entries of the jar file package and put them in the final jar ZipEntry entry; while ((entry = zis.getNextEntry()) != null) { // do not take directories or anything inside a potential META-INF folder. if (entry.isDirectory() || !entry.getName().endsWith(".class")) { continue; } String name = entry.getName(); String className = getClassName(name); if (removeClasses.contains(className)) { continue; } JarEntry newEntry; // Preserve the STORED method of the input entry. if (entry.getMethod() == JarEntry.STORED) { newEntry = new JarEntry(entry); } else { // Create a new entry so that the compressed len is recomputed. newEntry = new JarEntry(name); } // add the entry to the jar archive jos.putNextEntry(newEntry); // read the content of the entry from the input stream, and write it into the archive. int count; while ((count = zis.read(buffer)) != -1) { jos.write(buffer, 0, count); } // close the entries for this file jos.closeEntry(); zis.closeEntry(); } } finally { zis.close(); } fis.close(); jos.close(); FileUtils.deleteQuietly(inJar); FileUtils.moveFile(outJar, inJar); }
From source file:net.rptools.lib.FileUtil.java
public static void unzip(ZipInputStream in, File destDir) throws IOException { if (in == null) throw new IOException("input stream cannot be null"); // Prepare destination destDir.mkdirs();/*from w ww . ja va2 s . c om*/ File absDestDir = destDir.getAbsoluteFile(); // Pull out the files OutputStream out = null; ZipEntry entry = null; try { while ((entry = in.getNextEntry()) != null) { if (entry.isDirectory()) continue; // Prepare file destination File entryFile = new File(absDestDir, entry.getName()); entryFile.getParentFile().mkdirs(); out = new FileOutputStream(entryFile); IOUtils.copy(in, out); IOUtils.closeQuietly(out); in.closeEntry(); } } finally { IOUtils.closeQuietly(out); } }
From source file:com.glaf.core.util.ZipUtils.java
public static Map<String, byte[]> getZipBytesMap(ZipInputStream zipInputStream, List<String> includes) { Map<String, byte[]> zipMap = new java.util.HashMap<String, byte[]>(); java.util.zip.ZipEntry zipEntry = null; ByteArrayOutputStream baos = null; BufferedOutputStream bos = null; byte tmpByte[] = null; try {/*from ww w .j a va 2 s . com*/ while ((zipEntry = zipInputStream.getNextEntry()) != null) { String name = zipEntry.getName(); String ext = FileUtils.getFileExt(name); if (includes.contains(ext) || includes.contains(ext.toLowerCase())) { tmpByte = new byte[BUFFER]; baos = new ByteArrayOutputStream(); bos = new BufferedOutputStream(baos, BUFFER); int i = 0; while ((i = zipInputStream.read(tmpByte, 0, BUFFER)) != -1) { bos.write(tmpByte, 0, i); } bos.flush(); byte[] bytes = baos.toByteArray(); IOUtils.closeStream(baos); IOUtils.closeStream(baos); zipMap.put(zipEntry.getName(), bytes); } } } catch (Exception ex) { throw new RuntimeException(ex); } finally { IOUtils.closeStream(baos); IOUtils.closeStream(baos); } return zipMap; }
From source file:com.glaf.core.util.ZipUtils.java
public static void unzip(File zipFile) { FileInputStream fileInputStream = null; ZipInputStream zipInputStream = null; FileOutputStream fileoutputstream = null; BufferedOutputStream bufferedoutputstream = null; try {//ww w . j a v a 2 s .c o m fileInputStream = new FileInputStream(zipFile); zipInputStream = new ZipInputStream(fileInputStream); java.util.zip.ZipEntry zipEntry; while ((zipEntry = zipInputStream.getNextEntry()) != null) { byte abyte0[] = new byte[BUFFER]; fileoutputstream = new FileOutputStream(zipEntry.getName()); bufferedoutputstream = new BufferedOutputStream(fileoutputstream, BUFFER); int i; while ((i = zipInputStream.read(abyte0, 0, BUFFER)) != -1) { bufferedoutputstream.write(abyte0, 0, i); } bufferedoutputstream.flush(); IOUtils.closeStream(fileoutputstream); IOUtils.closeStream(bufferedoutputstream); } } catch (Exception ex) { throw new RuntimeException(ex); } finally { IOUtils.closeStream(fileInputStream); IOUtils.closeStream(zipInputStream); IOUtils.closeStream(fileoutputstream); IOUtils.closeStream(bufferedoutputstream); } }
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 w w . ja v a 2 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:gov.nasa.ensemble.resources.ResourceUtil.java
public static IProject unzipProject(String projectName, ZipInputStream zis, IProgressMonitor monitor) throws CoreException, IOException { final String dirPath = ResourcesPlugin.getWorkspace().getRoot().getLocation().toFile().getAbsolutePath(); if (monitor == null) monitor = new NullProgressMonitor(); try {//from w w w . j av a 2 s .c o m monitor.beginTask("Loading " + projectName, 300); ZipEntry entry; while ((entry = zis.getNextEntry()) != null) { File to = (!new File(dirPath, entry.getName()).getAbsolutePath().contains(projectName)) ? new File(dirPath + File.separator + projectName, entry.getName()) : new File(dirPath, entry.getName()); if (entry.isDirectory()) { if (!to.exists() && !to.mkdirs()) { throw new IOException("Error creating directory: " + to); } } else { File parent = to.getParentFile(); if (parent != null && !parent.exists() && !parent.mkdirs()) { throw new IOException("Error creating directory: " + parent); } FileOutputStream fos = new FileOutputStream(to); try { monitor.subTask("Expanding: " + to.getName()); IOUtils.copy(zis, fos); } finally { IOUtils.closeQuietly(fos); } } monitor.worked(10); } IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(projectName); project.create(null); project.open(null); return project; } catch (IOException e) { throw new CoreException( new ExceptionStatus(EnsembleResourcesPlugin.PLUGIN_ID, "adding project to workspace", e)); } finally { zis.close(); monitor.done(); } }
From source file:it.geosolutions.tools.compress.file.Extractor.java
public static void unZip(File inputZipFile, File outputDir) throws IOException, CompressorException { if (inputZipFile == null || outputDir == null) { throw new CompressorException("Unzip: with null parameters"); }/*from w w w . j a va2 s . c o m*/ if (!outputDir.exists()) { if (!outputDir.mkdirs()) { throw new CompressorException("Unzip: Unable to create directory structure: " + outputDir); } } final int BUFFER = Conf.getBufferSize(); ZipInputStream zipInputStream = null; try { // Open Zip file for reading zipInputStream = new ZipInputStream(new FileInputStream(inputZipFile)); } catch (FileNotFoundException fnf) { throw new CompressorException("Unzip: Unable to find the input zip file named: " + inputZipFile); } // extract file if not a directory BufferedInputStream bis = new BufferedInputStream(zipInputStream); // grab a zip file entry ZipEntry entry = null; while ((entry = zipInputStream.getNextEntry()) != null) { // Process each entry File currentFile = new File(outputDir, entry.getName()); FileOutputStream fos = null; BufferedOutputStream dest = null; try { int currentByte; // establish buffer for writing file byte data[] = new byte[BUFFER]; // write the current file to disk fos = new FileOutputStream(currentFile); dest = new BufferedOutputStream(fos, BUFFER); // read and write until last byte is encountered while ((currentByte = bis.read(data, 0, BUFFER)) != -1) { dest.write(data, 0, currentByte); } } catch (IOException ioe) { } finally { try { if (dest != null) { dest.flush(); dest.close(); } if (fos != null) fos.close(); } catch (IOException ioe) { throw new CompressorException( "Unzip: unable to close the zipInputStream: " + ioe.getLocalizedMessage()); } } } try { if (zipInputStream != null) zipInputStream.close(); } catch (IOException ioe) { throw new CompressorException( "Unzip: unable to close the zipInputStream: " + ioe.getLocalizedMessage()); } try { if (bis != null) bis.close(); } catch (IOException ioe) { throw new CompressorException( "Unzip: unable to close the Buffered zipInputStream: " + ioe.getLocalizedMessage()); } }
From source file:isl.FIMS.utils.Utils.java
/** * Unzip it (This implementation works only when zip contains files-folders * with ASCII filenames Greek characters break the code! * * @param zipFile input zip file/*from www . j a v a2 s . c o m*/ * @param output zip file output folder */ public static void unzip(String zipFile, String outputFolder) { String rootFolderName = ""; String rootFlashFilename = ""; byte[] buffer = new byte[1024]; try { //get the zip file content ZipInputStream zis = new ZipInputStream(new FileInputStream(outputFolder + File.separator + zipFile)); //get the zipped file list entry ZipEntry ze = zis.getNextEntry(); boolean rootDirFound = false; boolean flashFileFound = false; 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 if (!ze.getName().contains("__MACOSX")) { if (ze.isDirectory()) { if (rootDirFound == false) { rootFolderName = newFile.getName(); rootDirFound = true; } new File(newFile.getParent()).mkdirs(); } else { FileOutputStream fos = null; new File(newFile.getParent()).mkdirs(); if (flashFileFound == false && newFile.getName().endsWith(".swf") && !newFile.getName().startsWith(".")) { rootFlashFilename = newFile.getName(); flashFileFound = true; } 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 ex) { ex.printStackTrace(); } }
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 w w. j a v a 2 s. c om 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); } }