List of usage examples for java.util.zip ZipInputStream closeEntry
public void closeEntry() throws IOException
From source file:edu.cmu.tetrad.util.TetradSerializableUtils.java
/** * Deserializes examplars stored in archives in getArchiveDirectory(). * * @throws RuntimeException if clazz cannot be serialized. This exception * has an informative message and wraps the * originally thrown exception as root cause. * @see #getArchiveDirectory()//from w ww .j a v a2 s . c o m */ public void deserializeArchivedVersions() throws RuntimeException { System.out.println("Deserializing archived instances in " + getArchiveDirectory() + "."); File archive = new File(getArchiveDirectory()); if (!archive.exists() || !archive.isDirectory()) { return; } String[] listing = archive.list(); for (String archiveName : listing) { if (!(archiveName.endsWith(".zip"))) { continue; } try { File file = new File(getArchiveDirectory(), archiveName); ZipFile zipFile = new ZipFile(file); ZipEntry entry = zipFile.getEntry("class_fields.ser"); InputStream inputStream = zipFile.getInputStream(entry); ObjectInputStream objectIn = new ObjectInputStream(inputStream); Map<String, List<String>> classFields = (Map<String, List<String>>) objectIn.readObject(); zipFile.close(); for (String className : classFields.keySet()) { // if (classFields.equals("HypotheticalGraph")) continue; List<String> fieldNames = classFields.get(className); Class<?> clazz = Class.forName(className); ObjectStreamClass streamClass = ObjectStreamClass.lookup(clazz); if (streamClass == null) { System.out.println(); } for (String fieldName : fieldNames) { assert streamClass != null; ObjectStreamField field = streamClass.getField(fieldName); if (field == null) { throw new RuntimeException("Field '" + fieldName + "' was dropped from class '" + className + "' as a serializable field! Please " + "put it back!!!" + "\nIt used to be in " + className + " in this archive: " + archiveName + "."); } } } } catch (ClassNotFoundException e) { throw new RuntimeException("Could not read class_fields.ser in archive + " + archiveName + " .", e); } catch (IOException e) { throw new RuntimeException("Problem reading archive" + archiveName + "; see cause.", e); } System.out.println("...Deserializing instances in " + archiveName + "..."); ZipEntry zipEntry = null; try { File file = new File(getArchiveDirectory(), archiveName); FileInputStream in = new FileInputStream(file); ZipInputStream zipinputstream = new ZipInputStream(in); while ((zipEntry = zipinputstream.getNextEntry()) != null) { if (!zipEntry.getName().endsWith(".ser")) { continue; } ObjectInputStream objectIn = new ObjectInputStream(zipinputstream); objectIn.readObject(); zipinputstream.closeEntry(); } zipinputstream.close(); } catch (ClassNotFoundException e) { throw new RuntimeException( "Could not read object zipped file " + zipEntry.getName() + " in archive " + archiveName + ". " + "Perhaps the class was renamed, moved to another package, or " + "removed. In any case, please put it back where it was.", e); } catch (IOException e) { throw new RuntimeException("Problem reading archive" + archiveName + "; see cause.", e); } } System.out.println("Finished deserializing archived instances."); }
From source file:nl.nn.adapterframework.webcontrol.api.SendJmsMessage.java
private void processZipFile(InputStream file, JmsSender qms, String replyTo) throws IOException { ZipInputStream archive = new ZipInputStream(file); for (ZipEntry entry = archive.getNextEntry(); entry != null; entry = archive.getNextEntry()) { String name = entry.getName(); int size = (int) entry.getSize(); if (size > 0) { byte[] b = new byte[size]; int rb = 0; int chunk = 0; while ((size - rb) > 0) { chunk = archive.read(b, rb, size - rb); if (chunk == -1) { break; }/*from www. j av a2 s .c o m*/ rb += chunk; } String currentMessage = XmlUtils.readXml(b, 0, rb, Misc.DEFAULT_INPUT_STREAM_ENCODING, false); // initiate MessageSender if ((replyTo != null) && (replyTo.length() > 0)) qms.setReplyToName(replyTo); processMessage(qms, name + "_" + Misc.createSimpleUUID(), currentMessage); } archive.closeEntry(); } archive.close(); }
From source file:nl.nn.adapterframework.webcontrol.pipes.TestPipeLine.java
private String processZipFile(IPipeLineSession session, InputStream inputStream, String fileEncoding, IAdapter adapter, boolean writeSecLogMessage) throws IOException { String result = ""; String lastState = null;//from w ww. ja v a 2 s . com ZipInputStream archive = new ZipInputStream(inputStream); for (ZipEntry entry = archive.getNextEntry(); entry != null; entry = archive.getNextEntry()) { String name = entry.getName(); int size = (int) entry.getSize(); if (size > 0) { byte[] b = new byte[size]; int rb = 0; int chunk = 0; while (((int) size - rb) > 0) { chunk = archive.read(b, rb, (int) size - rb); if (chunk == -1) { break; } rb += chunk; } String message = XmlUtils.readXml(b, 0, rb, fileEncoding, false); if (StringUtils.isNotEmpty(result)) { result += "\n"; } lastState = processMessage(adapter, message, writeSecLogMessage).getState(); result += name + ":" + lastState; } archive.closeEntry(); } archive.close(); session.put("state", lastState); session.put("result", result); return ""; }
From source file:com.taobao.android.builder.tasks.app.databinding.AwbDataBindingMergeArtifactsTask.java
private void extractBinFilesFromJar(File outFolder, File jarFile) throws IOException { File jarOutFolder = getOutFolderForJarFile(outFolder, jarFile); FileUtils.deleteQuietly(jarOutFolder); FileUtils.forceMkdir(jarOutFolder);/*from w ww.jav a2 s . co m*/ try (Closer localCloser = Closer.create()) { FileInputStream fis = localCloser.register(new FileInputStream(jarFile)); ZipInputStream zis = localCloser.register(new ZipInputStream(fis)); ZipEntry entry; while ((entry = zis.getNextEntry()) != null) { if (entry.isDirectory()) { continue; } String name = entry.getName(); if (!isResource(name)) { continue; } // get rid of the path. We don't need it since the file name includes the domain name = new File(name).getName(); File out = new File(jarOutFolder, name); //noinspection ResultOfMethodCallIgnored FileOutputStream fos = localCloser.register(new FileOutputStream(out)); ByteStreams.copy(zis, fos); zis.closeEntry(); } } }
From source file:org.geoserver.wfs.response.ShapeZipTest.java
/** * Asserts the contents for the file named {@code fileName} contained in the zip file given by the {@code zippedIn} * matched the {@code expectedContent}/*from w w w . ja v a2 s. co m*/ */ private void checkFileContent(final String fileName, final InputStream zippedIn, final String expectedContent) throws IOException { ZipInputStream zis = new ZipInputStream(zippedIn); ZipEntry entry = null; try { while ((entry = zis.getNextEntry()) != null) { try { final String name = entry.getName(); if (name.toLowerCase().endsWith(fileName.toLowerCase())) { String unzippedFileContents = IOUtils.toString(zis); assertEquals(expectedContent, unzippedFileContents); return; } } finally { zis.closeEntry(); } } } finally { zis.close(); } fail(fileName + " was not found in the provided stream"); }
From source file:com.taobao.android.tpatch.utils.JarSplitUtils.java
/** * jarclass//from w w w . j a v a 2 s.c o 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:org.danann.cernunnos.io.ArchiveIteratorTask.java
public void perform(TaskRequest req, TaskResponse res) { URL url = resource.evaluate(req, res); ZipInputStream zip = null; try {/* w w w.j ava 2 s. co m*/ final String urlExternalForm = url.toExternalForm(); try { zip = new ZipInputStream(new BufferedInputStream(url.openStream())); } catch (IOException ioe) { throw new RuntimeException("Failed to open input stream for URL: " + urlExternalForm, ioe); } // Set the default CONTEXT for subtasks... res.setAttribute(Attributes.CONTEXT, "jar:" + urlExternalForm + "!/"); try { for (ZipEntry entry = zip.getNextEntry(); entry != null; entry = zip.getNextEntry()) { if (entry.isDirectory()) { // We need to skip these b/c many possible subtasks will // choke on them. Hope that doesn't become a problem. continue; } res.setAttribute(Attributes.LOCATION, entry.getName()); super.performSubtasks(req, res); //Outer try/catch should never see an IOException from performSubtasks zip.closeEntry(); } } catch (IOException ioe) { throw new RuntimeException("Failed to read specified archive: " + urlExternalForm, ioe); } } finally { //Problems closing an InputStream can be ignored IOUtils.closeQuietly(zip); } }
From source file:org.scify.talkandplay.utils.Updater.java
private ArrayList<String> extractZip() { ArrayList<String> tempfilesThatWillReplaceTheExisting = new ArrayList<>(); try {// w ww .java 2 s. c o m ZipInputStream zipIn = new ZipInputStream( new FileInputStream(properties.getTmpFolder() + File.separator + properties.getZipFile())); ZipEntry entry = zipIn.getNextEntry(); // iterates over entries in the zip file while (entry != null) { String filePath = properties.getTmpFolder() + File.separator + entry.getName(); if (!entry.isDirectory()) { tempfilesThatWillReplaceTheExisting.add(filePath); BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(filePath)); byte[] bytesIn = new byte[1024]; int read = 0; while ((read = zipIn.read(bytesIn)) != -1) { bos.write(bytesIn, 0, read); } bos.close(); } else { File dir = new File(filePath); dir.mkdir(); } zipIn.closeEntry(); entry = zipIn.getNextEntry(); } zipIn.close(); } catch (FileNotFoundException ex) { Logger.getLogger(Updater.class.getName()).log(Level.SEVERE, null, ex); return null; } catch (IOException ex) { Logger.getLogger(Updater.class.getName()).log(Level.SEVERE, null, ex); return null; } return tempfilesThatWillReplaceTheExisting; }
From source file:nl.nn.adapterframework.webcontrol.api.TestPipeline.java
private void processZipFile(Map<String, Object> returnResult, InputStream inputStream, String fileEncoding, IAdapter adapter, boolean writeSecLogMessage) throws IOException { String result = ""; String lastState = null;//from w w w. ja v a 2 s. co m ZipInputStream archive = new ZipInputStream(inputStream); for (ZipEntry entry = archive.getNextEntry(); entry != null; entry = archive.getNextEntry()) { String name = entry.getName(); int size = (int) entry.getSize(); if (size > 0) { byte[] b = new byte[size]; int rb = 0; int chunk = 0; while (((int) size - rb) > 0) { chunk = archive.read(b, rb, (int) size - rb); if (chunk == -1) { break; } rb += chunk; } String message = XmlUtils.readXml(b, 0, rb, fileEncoding, false); if (StringUtils.isNotEmpty(result)) { result += "\n"; } lastState = processMessage(adapter, message, writeSecLogMessage).getState(); result += name + ":" + lastState; } archive.closeEntry(); } archive.close(); returnResult.put("state", lastState); returnResult.put("result", result); }
From source file:net.ymate.platform.commons.util.ClassUtils.java
@SuppressWarnings("unchecked") protected static <T> void __doFindClassByZip(Collection<Class<T>> collections, Class<T> clazz, String packageName, URL zipUrl, Class<?> callingClass) { ZipInputStream _zipStream = null; try {//from w ww . j av a 2 s. c o m String _zipFilePath = zipUrl.toString(); if (_zipFilePath.indexOf('!') > 0) { _zipFilePath = StringUtils.substringBetween(zipUrl.toString(), "zip:", "!"); } else { _zipFilePath = StringUtils.substringAfter(zipUrl.toString(), "zip:"); } _zipStream = new ZipInputStream(new FileInputStream(new File(_zipFilePath))); ZipEntry _zipEntry = null; while (null != (_zipEntry = _zipStream.getNextEntry())) { if (!_zipEntry.isDirectory()) { if (_zipEntry.getName().endsWith(".class") && _zipEntry.getName().indexOf('$') < 0) { Class<?> _class = __doProcessEntry(zipUrl, _zipEntry); if (_class != null) { if (clazz.isAnnotation()) { if (isAnnotationOf(_class, (Class<Annotation>) clazz)) { collections.add((Class<T>) _class); } } else if (clazz.isInterface()) { if (isInterfaceOf(_class, clazz)) { collections.add((Class<T>) _class); } } else if (isSubclassOf(_class, clazz)) { collections.add((Class<T>) _class); } } } } _zipStream.closeEntry(); } } catch (Exception e) { _LOG.warn("", RuntimeUtils.unwrapThrow(e)); } finally { if (_zipStream != null) { try { _zipStream.close(); } catch (IOException e) { _LOG.warn("", RuntimeUtils.unwrapThrow(e)); } } } }