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:com.gatf.generator.core.GatfTestGeneratorMojo.java
/** * @param zipFile// ww w . j a v a2s . c om * @param directoryToExtractTo Provides file unzip functionality */ public void unzipZipFile(InputStream zipFile, String directoryToExtractTo) { ZipInputStream in = new ZipInputStream(zipFile); try { File directory = new File(directoryToExtractTo); if (!directory.exists()) { directory.mkdirs(); getLog().info("Creating directory for Extraction..."); } ZipEntry entry = in.getNextEntry(); while (entry != null) { try { File file = new File(directory, entry.getName()); if (entry.isDirectory()) { file.mkdirs(); } else { FileOutputStream out = new FileOutputStream(file); byte[] buffer = new byte[2048]; int len; while ((len = in.read(buffer)) > 0) { out.write(buffer, 0, len); } out.close(); } in.closeEntry(); entry = in.getNextEntry(); } catch (Exception e) { getLog().error(e); } } } catch (IOException ioe) { getLog().error(ioe); return; } }
From source file:ingest.utility.IngestUtilities.java
private void extractZipEntry(final String fileName, final ZipInputStream zipInputStream, final String zipPath, final String extractPath) throws IOException { byte[] buffer = new byte[1024]; String extension = FilenameUtils.getExtension(fileName); String filePath = String.format("%s%s%s.%s", extractPath, File.separator, "ShapefileData", extension); // Sanitize - blacklist if (filePath.contains("..") || (fileName.contains("/")) || (fileName.contains("\\"))) { logger.log(String.format( "Cannot extract Zip entry %s because it contains a restricted path reference. Characters such as '..' or slashes are disallowed. The initial zip path was %s. This was blocked to prevent a vulnerability.", fileName, zipPath), Severity.WARNING, new AuditElement(INGEST, "restrictedPathDetected", zipPath)); zipInputStream.closeEntry();/*from ww w . ja v a 2 s. c o m*/ return; } // Sanitize - whitelist boolean filePathContainsExtension = false; for (final String ext : Arrays.asList(".shp", ".prj", ".shx", ".dbf", ".sbn")) { if (filePath.contains(ext)) { filePathContainsExtension = true; break; } } if (filePathContainsExtension) { File newFile = new File(filePath).getCanonicalFile(); // Create all non existing folders new File(newFile.getParent()).mkdirs(); try (FileOutputStream outputStream = new FileOutputStream(newFile)) { int length; while ((length = zipInputStream.read(buffer)) > 0) { outputStream.write(buffer, 0, length); } } zipInputStream.closeEntry(); } else { zipInputStream.closeEntry(); } }
From source file:fr.inria.atlanmod.neo4emf.neo4jresolver.runtimes.internal.Neo4jZippedInstaller.java
@Override public void performInstall(IProgressMonitor monitor, IPath dirPath) throws IOException { if (monitor == null) { monitor = new NullProgressMonitor(); }/* w w w .j av a 2 s . c o m*/ InputStream urlStream = null; try { URL url = getUrl(); URLConnection connection = url.openConnection(); connection.setConnectTimeout(TIMEOUT); connection.setReadTimeout(TIMEOUT); int length = connection.getContentLength(); monitor.beginTask(NLS.bind("Reading from {1}", getVersion(), getUrl().toString()), length >= 0 ? length : IProgressMonitor.UNKNOWN); urlStream = connection.getInputStream(); ZipInputStream zipStream = new ZipInputStream(urlStream); byte[] buffer = new byte[1024 * 8]; long start = System.currentTimeMillis(); int total = length; int totalRead = 0; ZipEntry entry; float kBps = -1; while ((entry = zipStream.getNextEntry()) != null) { if (monitor.isCanceled()) break; String fullFilename = entry.getName(); IPath fullFilenamePath = new Path(fullFilename); int secsRemaining = (int) ((total - totalRead) / 1024 / kBps); String textRemaining = secsToText(secsRemaining); monitor.subTask(NLS.bind("{0} remaining. Reading {1}", textRemaining.length() > 0 ? textRemaining : "unknown time", StringUtils .abbreviateMiddle(fullFilenamePath.removeFirstSegments(1).toString(), "...", 45))); int entrySize = (int) entry.getCompressedSize(); OutputStream output = null; try { int len = 0; int read = 0; String action = null; if (jarFiles.contains(fullFilename)) { action = "Copying"; String filename = FilenameUtils.getName(fullFilename); output = new FileOutputStream(dirPath.append(filename).toOSString()); } else { action = "Skipping"; output = new NullOutputStream(); } int secs = (int) ((System.currentTimeMillis() - start) / 1000); kBps = (float) totalRead / 1024 / secs; while ((len = zipStream.read(buffer)) > 0) { if (monitor.isCanceled()) break; read += len; monitor.subTask(NLS.bind("{0} remaining. {1} {2} at {3}KB/s ({4}KB / {5}KB)", new Object[] { String.format("%s", textRemaining.length() > 0 ? textRemaining : "unknown time"), action, StringUtils.abbreviateMiddle( fullFilenamePath.removeFirstSegments(1).toString(), "...", 45), String.format("%,.1f", kBps), String.format("%,.1f", (float) read / 1024), String.format("%,.1f", (float) entry.getSize() / 1024) })); output.write(buffer, 0, len); } totalRead += entrySize; monitor.worked(entrySize); } finally { IOUtils.closeQuietly(output); } } } finally { IOUtils.closeQuietly(urlStream); monitor.done(); } }
From source file:org.openbravo.erpCommon.modules.ImportModule.java
private byte[] getBytesCurrentEntryStream(ZipInputStream obxInputStream) throws Exception { final ByteArrayOutputStream fout = new ByteArrayOutputStream(); final byte[] buf = new byte[1024]; int len;/* w ww.j a v a 2 s. co m*/ while ((len = obxInputStream.read(buf)) > 0) { fout.write(buf, 0, len); } fout.close(); return fout.toByteArray(); }
From source file:pl.psnc.synat.wrdz.zmd.object.parser.ObjectModificationParser.java
/** * Parses the request provided as ZIP./*from w ww. ja va2 s . co m*/ * * @param identifier * identifier of the object * @param zis * input stream of the zip request * @return internal representation of the request * @throws IncompleteDataException * when some data are missing * @throws InvalidDataException * when some data are invalid */ public ObjectModificationRequest parse(String identifier, ZipInputStream zis) throws IncompleteDataException, InvalidDataException { ObjectModificationRequestBuilder requestBuilder = new ObjectModificationRequestBuilder(identifier); requestBuilder.setDeleteAllContent(true); if (!new File(root).mkdir()) { logger.error("Root folder " + root + " creation failed."); throw new WrdzRuntimeException("Root folder " + root + " creation failed."); } ZipEntry entry = null; try { while ((entry = zis.getNextEntry()) != null) { logger.debug("unzipping: " + entry.getName() + " - is directory: " + entry.isDirectory()); if (entry.isDirectory()) { if (!new File(root + "/" + entry.getName()).mkdir()) { logger.error("folder " + entry.getName() + " creation failed."); throw new WrdzRuntimeException("Folder " + entry.getName() + " creation failed."); } } else { BufferedOutputStream bos = new BufferedOutputStream( new FileOutputStream(root + "/" + entry.getName()), 1024); int len; byte[] buffer = new byte[1024]; while ((len = zis.read(buffer)) != -1) { bos.write(buffer, 0, len); } bos.flush(); bos.close(); if (entry.getName().toLowerCase().startsWith("metadata/")) { String metadataName = entry.getName().substring(new String("metadata/").length()); if (metadataName.contains("/")) { requestBuilder.addInputFileToAddMetadataFile( metadataName.substring(0, metadataName.lastIndexOf('/')), ObjectManagerRequestHelper.makeUri(root, entry.getName())); } else { requestBuilder.addObjectMetadataToAdd( ObjectManagerRequestHelper.makeUri(root, entry.getName())); } } else { String contentName = entry.getName(); if (contentName.toLowerCase().startsWith("content/")) { contentName = contentName.substring(new String("content/").length()); } requestBuilder.setInputFileToAddSource(contentName, ObjectManagerRequestHelper.makeUri(root, entry.getName())); requestBuilder.setInputFileToAddDestination(contentName, contentName); } } zis.closeEntry(); } zis.close(); } catch (FileNotFoundException e) { logger.error("Saving the file " + entry.getName() + " failed!"); throw new WrdzRuntimeException(e.getMessage()); } catch (IOException e) { logger.error("Uncorrect zip file.", e); FileUtils.deleteQuietly(new File(root)); throw new InvalidDataException(e.getMessage()); } return requestBuilder.build(); }
From source file:org.wso2.carbon.greg.soap.viewer.WSDLVisualizer.java
/** * Get the temp location of output directory where wsdl files is saved with its dependencies * * @param path Registry path the the WSDL file * @return Output directory path/*from ww w .j a va 2 s. c om*/ */ private String writeResourceToFile(String path) { String outDir = null; try { Registry registry = ServiceHolder.getRegistryService().getSystemRegistry(); ContentDownloadBean zipContentBean = ContentUtil.getContentWithDependencies(path, (UserRegistry) registry); InputStream zipContentStream = zipContentBean.getContent().getInputStream(); ZipInputStream stream = new ZipInputStream(zipContentStream); byte[] buffer = new byte[2048]; String uniqueID = UUID.randomUUID().toString(); outDir = CarbonUtils.getCarbonHome() + File.separator + "tmp" + File.separator + uniqueID; (new File(outDir)).mkdir(); ZipEntry entry; while ((entry = stream.getNextEntry()) != null) { String s = String.format("Entry: %s len %d added %TD", outDir + File.separator + entry.getName(), entry.getSize(), new Date(entry.getTime())); String outPath = outDir + File.separator + entry.getName(); (new File(outPath)).getParentFile().mkdirs(); FileOutputStream output = null; try { output = new FileOutputStream(outPath); int len = 0; while ((len = stream.read(buffer)) > 0) { output.write(buffer, 0, len); } } finally { // Close the output file if (output != null) { output.close(); } } } } catch (FileNotFoundException e) { logger.error("temporary output directory cannot be found ", e); } catch (IOException e) { logger.error("Error occurred while writing the WSDL content to the temporary file", e); } catch (RegistryException e) { logger.error("Error occurred while getting registry from service holder", e); } catch (Exception e) { logger.error("Error occurred while getting registry from service holder", e); } return outDir; }
From source file:org.wso2.carbon.governance.platform.extensions.handlers.RESTZipWSDLMediaTypeHandler.java
public void put(RequestContext requestContext) throws RegistryException { if (!CommonUtil.isUpdateLockAvailable()) { return;/* ww w . j av a 2s . co m*/ } CommonUtil.acquireUpdateLock(); try { Resource resource = requestContext.getResource(); String path = requestContext.getResourcePath().getPath(); try { if (resource != null) { Object resourceContent = resource.getContent(); InputStream in = new ByteArrayInputStream((byte[]) resourceContent); Stack<File> fileList = new Stack<File>(); List<String> uriList = new LinkedList<String>(); List<UploadTask> tasks = new LinkedList<UploadTask>(); int threadPoolSize = this.threadPoolSize; int wsdlPathDepth = Integer.MAX_VALUE; int xsdPathDepth = Integer.MAX_VALUE; File tempFile = File.createTempFile(tempFilePrefix, archiveExtension); File tempDir = new File(tempFile.getAbsolutePath().substring(0, tempFile.getAbsolutePath().length() - archiveExtension.length())); try { BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(tempFile)); try { byte[] contentChunk = new byte[1024]; int byteCount; while ((byteCount = in.read(contentChunk)) != -1) { out.write(contentChunk, 0, byteCount); } out.flush(); } finally { out.close(); } ZipEntry entry; makeDir(tempDir); ZipInputStream zs; List<String> wsdlUriList = new LinkedList<String>(); List<String> xsdUriList = new LinkedList<String>(); zs = new ZipInputStream(new FileInputStream(tempFile)); try { entry = zs.getNextEntry(); while (entry != null) { String entryName = entry.getName(); FileOutputStream os; File file = new File(tempFile.getAbsolutePath().substring(0, tempFile.getAbsolutePath().length() - archiveExtension.length()) + File.separator + entryName); if (entry.isDirectory()) { if (!file.exists()) { makeDirs(file); fileList.push(file); } entry = zs.getNextEntry(); continue; } File parentFile = file.getParentFile(); if (!parentFile.exists()) { makeDirs(parentFile); } os = new FileOutputStream(file); try { fileList.push(file); byte[] contentChunk = new byte[1024]; int byteCount; while ((byteCount = zs.read(contentChunk)) != -1) { os.write(contentChunk, 0, byteCount); } } finally { os.close(); } zs.closeEntry(); entry = zs.getNextEntry(); if (entryName != null && entryName.toLowerCase().endsWith(wsdlExtension)) { String uri = tempFile.toURI().toString(); uri = uri.substring(0, uri.length() - archiveExtension.length()) + "/" + entryName; if (uri.startsWith("file:")) { uri = uri.substring(5); } while (uri.startsWith("/")) { uri = uri.substring(1); } uri = "file:///" + uri; if (uri.endsWith("/")) { uri = uri.substring(0, uri.length() - 1); } int uriPathDepth = uri.split("/").length; if (uriPathDepth < wsdlPathDepth) { wsdlPathDepth = uriPathDepth; wsdlUriList = new LinkedList<String>(); } if (wsdlPathDepth == uriPathDepth) { wsdlUriList.add(uri); } } else if (entryName != null && entryName.toLowerCase().endsWith(xsdExtension)) { String uri = tempFile.toURI().toString(); uri = uri.substring(0, uri.length() - archiveExtension.length()) + "/" + entryName; if (uri.startsWith("file:")) { uri = uri.substring(5); } while (uri.startsWith("/")) { uri = uri.substring(1); } uri = "file:///" + uri; if (uri.endsWith("/")) { uri = uri.substring(0, uri.length() - 1); } int uriPathDepth = uri.split("/").length; if (uriPathDepth < xsdPathDepth) { xsdPathDepth = uriPathDepth; xsdUriList = new LinkedList<String>(); } if (xsdPathDepth == uriPathDepth) { xsdUriList.add(uri); } } else if (entryName != null) { String uri = tempFile.toURI().toString(); uri = uri.substring(0, uri.length() - archiveExtension.length()) + "/" + entryName; if (uri.startsWith("file:")) { uri = uri.substring(5); } while (uri.startsWith("/")) { uri = uri.substring(1); } uri = "file:///" + uri; if (uri.endsWith("/")) { uri = uri.substring(0, uri.length() - 1); } uriList.add(uri); } } } finally { zs.close(); } Map<String, String> localPathMap = null; if (CurrentSession.getLocalPathMap() != null) { localPathMap = Collections.unmodifiableMap(CurrentSession.getLocalPathMap()); } if (wsdlUriList.isEmpty() && xsdUriList.isEmpty()) { throw new RegistryException("No WSDLs or Schemas found in the given WSDL archive"); } if (wsdlPathDepth < Integer.MAX_VALUE) { for (String uri : wsdlUriList) { tasks.add(new UploadWSDLTask(requestContext, uri, CurrentSession.getTenantId(), CurrentSession.getUserRegistry(), CurrentSession.getUserRealm(), CurrentSession.getUser(), CurrentSession.getCallerTenantId(), localPathMap)); } } if (xsdPathDepth < Integer.MAX_VALUE) { for (String uri : xsdUriList) { tasks.add(new UploadXSDTask(requestContext, uri, CurrentSession.getTenantId(), CurrentSession.getUserRegistry(), CurrentSession.getUserRealm(), CurrentSession.getUser(), CurrentSession.getCallerTenantId(), localPathMap)); } } for (String uri : uriList) { if (uri.endsWith(restExtension)) { tasks.add(new UploadRESTModelTask(requestContext, uri, CurrentSession.getTenantId(), CurrentSession.getUserRegistry(), CurrentSession.getUserRealm(), CurrentSession.getUser(), CurrentSession.getCallerTenantId(), localPathMap)); } } // calculate thread pool size for efficient use of resources in concurrent // update scenarios. int toAdd = wsdlUriList.size() + xsdUriList.size(); if (toAdd < threadPoolSize) { if (toAdd < (threadPoolSize / 8)) { threadPoolSize = 0; } else if (toAdd < (threadPoolSize / 2)) { threadPoolSize = (threadPoolSize / 8); } else { threadPoolSize = (threadPoolSize / 4); } } } finally { in.close(); resourceContent = null; resource.setContent(null); } uploadFiles(tasks, tempFile, fileList, tempDir, threadPoolSize, path, uriList, requestContext); } } catch (IOException e) { throw new RegistryException("Error occurred while unpacking Governance Archive", e); } requestContext.setProcessingComplete(true); } finally { CommonUtil.releaseUpdateLock(); } }
From source file:org.wso2.carbon.governance.soap.viewer.WSDLVisualizer.java
/** * Get the temp location of output directory where wsdl files is saved with its dependencies * * @param path Registry path the the WSDL file * @return Output directory path/*www . j ava 2 s.c o m*/ */ private String writeResourceToFile(String path, int tenantId) { String outDir = null; try { UserRegistry registry = ServiceHolder.getRegistryService().getSystemRegistry(tenantId); ContentDownloadBean zipContentBean = ContentUtil.getContentWithDependencies(path, registry); InputStream zipContentStream = zipContentBean.getContent().getInputStream(); ZipInputStream stream = new ZipInputStream(zipContentStream); byte[] buffer = new byte[2048]; String uniqueID = UUID.randomUUID().toString(); outDir = CarbonUtils.getCarbonHome() + File.separator + "tmp" + File.separator + uniqueID; (new File(outDir)).mkdir(); ZipEntry entry; while ((entry = stream.getNextEntry()) != null) { String s = String.format("Entry: %s len %d added %TD", outDir + File.separator + entry.getName(), entry.getSize(), new Date(entry.getTime())); String outPath = outDir + File.separator + entry.getName(); (new File(outPath)).getParentFile().mkdirs(); FileOutputStream output = null; try { output = new FileOutputStream(outPath); int len = 0; while ((len = stream.read(buffer)) > 0) { output.write(buffer, 0, len); } } finally { // Close the output file if (output != null) { output.close(); } } } } catch (FileNotFoundException e) { logger.error("temporary output directory cannot be found ", e); } catch (IOException e) { logger.error("Error occurred while writing the WSDL content to the temporary file", e); } catch (RegistryException e) { logger.error("Error occurred while getting registry from service holder", e); } catch (Exception e) { logger.error("Error occurred while getting registry from service holder", e); } return outDir; }
From source file:com.adobe.phonegap.contentsync.Sync.java
private boolean unzipSync(File targetFile, String outputDirectory, ProgressEvent progress, CallbackContext callbackContext) { Log.d(LOG_TAG, "unzipSync called"); Log.d(LOG_TAG, "zip = " + targetFile.getAbsolutePath()); InputStream inputStream = null; ZipFile zip = null;//from ww w . j a va 2s . c o m boolean anyEntries = false; try { synchronized (progress) { if (progress.isAborted()) { return false; } } zip = new ZipFile(targetFile); // Since Cordova 3.3.0 and release of File plugins, files are accessed via cdvfile:// // Accept a path or a URI for the source zip. Uri zipUri = getUriForArg(targetFile.getAbsolutePath()); Uri outputUri = getUriForArg(outputDirectory); CordovaResourceApi resourceApi = webView.getResourceApi(); File tempFile = resourceApi.mapUriToFile(zipUri); if (tempFile == null || !tempFile.exists()) { sendErrorMessage("Zip file does not exist", UNZIP_ERROR, callbackContext); } File outputDir = resourceApi.mapUriToFile(outputUri); outputDirectory = outputDir.getAbsolutePath(); outputDirectory += outputDirectory.endsWith(File.separator) ? "" : File.separator; if (outputDir == null || (!outputDir.exists() && !outputDir.mkdirs())) { sendErrorMessage("Could not create output directory", UNZIP_ERROR, callbackContext); } OpenForReadResult zipFile = resourceApi.openForRead(zipUri); progress.setStatus(STATUS_EXTRACTING); progress.setLoaded(0); progress.setTotal(zip.size()); Log.d(LOG_TAG, "zip file len = " + zip.size()); inputStream = new BufferedInputStream(zipFile.inputStream); inputStream.mark(10); int magic = readInt(inputStream); if (magic != 875721283) { // CRX identifier inputStream.reset(); } else { // CRX files contain a header. This header consists of: // * 4 bytes of magic number // * 4 bytes of CRX format version, // * 4 bytes of public key length // * 4 bytes of signature length // * the public key // * the signature // and then the ordinary zip data follows. We skip over the header before creating the ZipInputStream. readInt(inputStream); // version == 2. int pubkeyLength = readInt(inputStream); int signatureLength = readInt(inputStream); inputStream.skip(pubkeyLength + signatureLength); } // The inputstream is now pointing at the start of the actual zip file content. ZipInputStream zis = new ZipInputStream(inputStream); inputStream = zis; ZipEntry ze; byte[] buffer = new byte[32 * 1024]; while ((ze = zis.getNextEntry()) != null) { synchronized (progress) { if (progress.isAborted()) { return false; } } anyEntries = true; String compressedName = ze.getName(); if (ze.getSize() > getFreeSpace()) { return false; } if (ze.isDirectory()) { File dir = new File(outputDirectory + compressedName); dir.mkdirs(); } else { File file = new File(outputDirectory + compressedName); file.getParentFile().mkdirs(); if (file.exists() || file.createNewFile()) { Log.w(LOG_TAG, "extracting: " + file.getPath()); FileOutputStream fout = new FileOutputStream(file); int count; while ((count = zis.read(buffer)) != -1) { fout.write(buffer, 0, count); } fout.close(); } } progress.addLoaded(1); updateProgress(callbackContext, progress); zis.closeEntry(); } } catch (Exception e) { String errorMessage = "An error occurred while unzipping."; sendErrorMessage(errorMessage, UNZIP_ERROR, callbackContext); Log.e(LOG_TAG, errorMessage, e); } finally { if (inputStream != null) { try { inputStream.close(); } catch (IOException e) { } } if (zip != null) { try { zip.close(); } catch (IOException e) { } } } if (anyEntries) return true; else return false; }