List of usage examples for java.util.zip ZipInputStream closeEntry
public void closeEntry() throws IOException
From source file:org.geoserver.wps.gs.download.DownloadProcessTest.java
/** * Private method for decoding a Shapefile * /*from w w w. j a va 2s . c o m*/ * @param input the input shp * @return the object a {@link SimpleFeatureCollection} object related to the shp file. * @throws Exception the exception */ private Object decodeShape(InputStream input) throws Exception { // create the temp directory and register it as a temporary resource File tempDir = IOUtils.createRandomDirectory(IOUtils.createTempDirectory("shpziptemp").getAbsolutePath(), "download-process", "download-services"); // unzip to the temporary directory ZipInputStream zis = null; File shapeFile = null; File zipFile = null; // extract shp-zip file try { zis = new ZipInputStream(input); ZipEntry entry = null; // Cycle on all the entries and copies the input shape in the target directory while ((entry = zis.getNextEntry()) != null) { String name = entry.getName(); File file = new File(tempDir, entry.getName()); if (entry.isDirectory()) { file.mkdir(); } else { if (file.getName().toLowerCase().endsWith(".shp")) { shapeFile = file; } else if (file.getName().toLowerCase().endsWith(".zip")) { zipFile = file; } int count; byte data[] = new byte[4096]; // write the files to the disk FileOutputStream fos = null; try { fos = new FileOutputStream(file); while ((count = zis.read(data)) != -1) { fos.write(data, 0, count); } fos.flush(); } finally { if (fos != null) { fos.close(); } } } zis.closeEntry(); } } finally { if (zis != null) { zis.close(); } } // Read the shapefile if (shapeFile == null) { if (zipFile != null) return decodeShape(new FileInputStream(zipFile)); else { FileUtils.deleteDirectory(tempDir); throw new IOException("Could not find any file with .shp extension in the zip file"); } } else { ShapefileDataStore store = new ShapefileDataStore(DataUtilities.fileToURL(shapeFile)); return store.getFeatureSource().getFeatures(); } }
From source file:org.talend.designer.runprocess.java.JavaProcessor.java
@Override public void generateEsbFiles() throws ProcessorException { List<? extends INode> graphicalNodes = process.getGraphicalNodes(); // process.getGeneratingNodes(); try {//from w w w. ja va 2 s.co m IPath jobPackagePath = getSrcCodePath().removeLastSegments(1); IFolder jobPackageFolder = this.getCodeProject().getFolder(jobPackagePath); IFolder wsdlsPackageFolder = jobPackageFolder.getFolder("wsdl"); //$NON-NLS-1$ if (wsdlsPackageFolder.exists()) { wsdlsPackageFolder.delete(true, null); } for (INode node : graphicalNodes) { if ("tESBConsumer".equals(node.getComponent().getName()) && node.isActivate()) { //$NON-NLS-1$ // retrieve WSDL content (compressed-n-encoded) -> zip-content.-> wsdls.(first named main.wsdl) String wsdlContent = (String) node.getPropertyValue("WSDL_CONTENT"); //$NON-NLS-1$ String uniqueName = node.getUniqueName(); if (null != uniqueName && null != wsdlContent && !wsdlContent.trim().isEmpty()) { // configure decoding and uncompressing InputStream wsdlStream = new BufferedInputStream(new InflaterInputStream( new Base64InputStream(new ByteArrayInputStream(wsdlContent.getBytes())))); if (!wsdlsPackageFolder.exists()) { wsdlsPackageFolder.create(true, true, null); } // generate WSDL file if (checkIsZipStream(wsdlStream)) { ZipInputStream zipIn = new ZipInputStream(wsdlStream); ZipEntry zipEntry = null; while ((zipEntry = zipIn.getNextEntry()) != null) { String outputName = zipEntry.getName(); if ("main.wsdl".equals(outputName)) { //$NON-NLS-1$ outputName = uniqueName + ".wsdl"; //$NON-NLS-1$ } IFile wsdlFile = wsdlsPackageFolder.getFile(outputName); if (!wsdlFile.exists()) { // cause create file will do a close. add a warp to ignore close. InputStream unCloseIn = new FilterInputStream(zipIn) { @Override public void close() throws IOException { }; }; wsdlFile.create(unCloseIn, true, null); } zipIn.closeEntry(); } zipIn.close(); } else { IFile wsdlFile = wsdlsPackageFolder.getFile(uniqueName + ".wsdl"); //$NON-NLS-1$ wsdlFile.create(wsdlStream, true, null); } } } } } catch (CoreException e) { if (e.getStatus() != null && e.getStatus().getException() != null) { ExceptionHandler.process(e.getStatus().getException()); } throw new ProcessorException(Messages.getString("Processor.tempFailed"), e); //$NON-NLS-1$ } catch (IOException e) { throw new ProcessorException(Messages.getString("Processor.tempFailed"), e); //$NON-NLS-1$ } boolean samEnabled = false; boolean slEnabled = false; for (INode node : graphicalNodes) { if (node.isActivate()) { final String nodeName = node.getComponent().getName(); Object slValue = null, samValue = null; if ("tESBConsumer".equals(nodeName) //$NON-NLS-1$ || "tRESTClient".equals(nodeName) //$NON-NLS-1$ || "tRESTRequest".equals(nodeName) //$NON-NLS-1$ || "cCXFRS".equals(nodeName)) { //$NON-NLS-1$ if (!slEnabled) { slValue = node.getPropertyValue("SERVICE_LOCATOR"); //$NON-NLS-1$ } if (!samEnabled) { samValue = node.getPropertyValue("SERVICE_ACTIVITY_MONITOR"); //$NON-NLS-1$ } } else if ("cCXF".equals(nodeName)) { //$NON-NLS-1$ if (!slEnabled) { slValue = node.getPropertyValue("ENABLE_SL"); //$NON-NLS-1$ } if (!samEnabled) { samValue = node.getPropertyValue("ENABLE_SAM"); //$NON-NLS-1$ } } if (null != slValue) { slEnabled = (Boolean) slValue; } if (null != samValue) { samEnabled = (Boolean) samValue; } if (samEnabled && slEnabled) { break; } } } if (samEnabled || slEnabled) { File esbConfigsSourceFolder = EsbConfigUtils.getEclipseEsbFolder(); if (!esbConfigsSourceFolder.exists()) { RunProcessPlugin.getDefault().getLog() .log(new Status(IStatus.WARNING, RunProcessPlugin.getDefault().getBundle().getSymbolicName(), "ESB configuration folder does not exists - " + esbConfigsSourceFolder.toURI())); //$NON-NLS-1$ return; } ITalendProcessJavaProject tProcessJvaProject = this.getTalendJavaProject(); if (tProcessJvaProject == null) { return; } IFolder esbConfigsTargetFolder = tProcessJvaProject.getResourcesFolder(); // add SAM config file to classpath if (samEnabled) { copyEsbConfigFile(esbConfigsSourceFolder, esbConfigsTargetFolder, "agent.properties"); //$NON-NLS-1$ } // add SL config file to classpath if (slEnabled) { copyEsbConfigFile(esbConfigsSourceFolder, esbConfigsTargetFolder, "locator.properties"); //$NON-NLS-1$ } } }
From source file:reconcile.hbase.mapreduce.ZipInputFormat.java
private List<ZipEntrySplit> getZipFileEntries(JobContext context, FileSystem fs, Path[] zipFiles, Integer maxEntryFiles, Integer ignoreFilesLargerThanMB, List<String> processMimeTypes) throws IOException { ArrayList<ZipEntrySplit> splits = new ArrayList<ZipEntrySplit>(); ZipInputStream zis = null; ZipEntry zipEntry = null;/*from www .j a v a2 s.c o m*/ for (int i = 0; i < zipFiles.length; i++) { Path file = zipFiles[i]; LOG.debug("Opening zip file: " + file.toString()); try { zis = new ZipInputStream(fs.open(file)); while ((zipEntry = zis.getNextEntry()) != null) { if (maxEntryFiles != null && splits.size() == maxEntryFiles.intValue()) { LOG.debug("Exceeded maximum number of splits. End getSplits()"); return splits; } boolean processFile = true; if (processMimeTypes.size() > 0) { // Ensure that if process mime types were specified, that entry // mime type meets that criteria String mimeType = fileNameMap.getContentTypeFor(zipEntry.getName()); if (mimeType == null || (!processMimeTypes.contains(mimeType.toLowerCase()))) { processFile = false; LOG.debug("Ignoring entry file (" + zipEntry.getName() + " mimeType(" + mimeType + ") not in process list"); } } long byteCount = zipEntry.getSize(); /* if (byteCount <= 0) { // Read entry and figure out size for ourselves byteCount = 0; while (zis.available()==1) { zis.read(); ++byteCount; } } */ if (ignoreFilesLargerThanMB != null && byteCount > ignoreFilesLargerThanMB.intValue()) { processFile = false; LOG.debug("Ignoring entry file (" + zipEntry.getName() + ") which exceeds size limit"); } if (processFile) { LOG.debug("Creating split for zip entry: " + zipEntry.getName() + " Size: " + byteCount + " Method: " + (ZipEntry.DEFLATED == zipEntry.getMethod() ? "DEFLATED" : "STORED") + " Compressed Size: " + zipEntry.getCompressedSize()); ZipEntrySplit zipSplit = new ZipEntrySplit(file, zipEntry.getName(), zipEntry.getSize(), context); splits.add(zipSplit); } zis.closeEntry(); } } finally { IOUtils.closeQuietly(zis); } } return splits; }
From source file:org.jahia.services.importexport.ImportExportBaseService.java
/** * Import a full site zip into a newly created site. * <p/>/* w w w.j a v a 2s .c o m*/ * zip file can contain all kind of legacy jahia import files or jcr import format. * * @param file Zip file * @param site The new site where to import * @param infos site infos * @param legacyMappingFilePath path to the legacy mappings * @param legacyDefinitionsFilePath path for the legacy definitions * @param session the current JCR session to use for the import * @throws RepositoryException * @throws IOException */ public void importSiteZip(Resource file, JahiaSite site, Map<Object, Object> infos, Resource legacyMappingFilePath, Resource legacyDefinitionsFilePath, JCRSessionWrapper session) throws RepositoryException, IOException { long timerSite = System.currentTimeMillis(); logger.info("Start import for site {}", site != null ? site.getSiteKey() : ""); final CategoriesImportHandler categoriesImportHandler = new CategoriesImportHandler(); final UsersImportHandler usersImportHandler = new UsersImportHandler(site, session); boolean legacyImport = false; List<String[]> catProps = null; List<String[]> userProps = null; Map<String, Long> sizes = new HashMap<String, Long>(); List<String> fileList = new ArrayList<String>(); logger.info("Start analyzing import file {}", file); long timer = System.currentTimeMillis(); getFileList(file, sizes, fileList); logger.info("Done analyzing import file {} in {}", file, DateUtils.formatDurationWords(System.currentTimeMillis() - timer)); Map<String, String> pathMapping = session.getPathMapping(); for (JahiaTemplatesPackage pkg : templatePackageRegistry.getRegisteredModules().values()) { String key = "/modules/" + pkg.getId() + "/"; if (!pathMapping.containsKey(key)) { pathMapping.put(key, "/modules/" + pkg.getId() + "/" + pkg.getVersion() + "/"); } } ZipInputStream zis; if (sizes.containsKey(USERS_XML)) { // Import users first zis = getZipInputStream(file); try { while (true) { ZipEntry zipentry = zis.getNextEntry(); if (zipentry == null) break; String name = zipentry.getName(); if (name.equals(USERS_XML)) { userProps = importUsers(zis, usersImportHandler); break; } zis.closeEntry(); } } finally { closeInputStream(zis); } } // Check if it is an 5.x or 6.1 import : for (Map.Entry<String, Long> entry : sizes.entrySet()) { if (entry.getKey().startsWith("export_")) { legacyImport = true; break; } } if (sizes.containsKey(SITE_PROPERTIES)) { zis = getZipInputStream(file); try { while (true) { ZipEntry zipentry = zis.getNextEntry(); if (zipentry == null) break; String name = zipentry.getName(); if (name.equals(SITE_PROPERTIES)) { importSiteProperties(zis, site, session); break; } zis.closeEntry(); } } finally { closeInputStream(zis); } } if (sizes.containsKey(REPOSITORY_XML)) { // Parse import file to detect sites zis = getZipInputStream(file); try { while (true) { ZipEntry zipentry = zis.getNextEntry(); if (zipentry == null) break; String name = zipentry.getName(); if (name.equals(REPOSITORY_XML)) { timer = System.currentTimeMillis(); logger.info("Start importing " + REPOSITORY_XML); DocumentViewValidationHandler h = new DocumentViewValidationHandler(); h.setSession(session); List<ImportValidator> validators = new ArrayList<ImportValidator>(); SitesValidator sitesValidator = new SitesValidator(); validators.add(sitesValidator); h.setValidators(validators); handleImport(zis, h, name); Map<String, Properties> sites = ((SitesValidatorResult) sitesValidator.getResult()) .getSitesProperties(); for (String s : sites.keySet()) { // Only the first site returned is mapped (if its not the systemsite, which is always the same key) if (!s.equals("systemsite") && !site.getSiteKey().equals("systemsite")) { // Map to the new sitekey pathMapping.put("/sites/" + s + "/", "/sites/" + site.getSiteKey() + "/"); break; } } if (!sizes.containsKey(SITE_PROPERTIES)) { // todo : site properties can be removed and properties get from here } logger.info("Done importing " + REPOSITORY_XML + " in {}", DateUtils.formatDurationWords(System.currentTimeMillis() - timer)); break; } zis.closeEntry(); } } finally { closeInputStream(zis); } importZip(null, file, DocumentViewImportHandler.ROOT_BEHAVIOUR_IGNORE, session, Sets.newHashSet(USERS_XML, CATEGORIES_XML), true); } else { // No repository descriptor - prepare to import files directly pathMapping.put("/", "/sites/" + site.getSiteKey() + "/files/"); } NodeTypeRegistry reg = NodeTypeRegistry.getInstance(); DefinitionsMapping mapping = null; // Import additional files - site.properties, old cateogries.xml , sitepermissions.xml // and eventual plain file from 5.x imports if (!sizes.containsKey(REPOSITORY_XML) || sizes.containsKey(SITE_PROPERTIES) || sizes.containsKey(CATEGORIES_XML) || sizes.containsKey(SITE_PERMISSIONS_XML) || sizes.containsKey(DEFINITIONS_CND) || sizes.containsKey(DEFINITIONS_MAP)) { zis = getZipInputStream(file); try { while (true) { ZipEntry zipentry = zis.getNextEntry(); if (zipentry == null) break; String name = zipentry.getName(); if (name.indexOf('\\') > -1) { name = name.replace('\\', '/'); } if (name.indexOf('/') > -1) { if (!sizes.containsKey(REPOSITORY_XML) && !sizes.containsKey(FILESACL_XML)) { // No repository descriptor - Old import format only name = "/" + name; if (name.startsWith("/content/sites")) { name = pathMapping.get("/") + StringUtils .stripStart(name.replaceFirst("/content/sites/[^/]+/files/", ""), "/"); } else if (name.startsWith("/users")) { Matcher m = Pattern.compile("/users/([^/]+)(/.*)?").matcher(name); if (m.matches()) { name = ServicesRegistry.getInstance().getJahiaUserManagerService() .getUserSplittingRule().getPathForUsername(m.group(1)); name = name + "/files" + ((m.group(2) != null) ? m.group(2) : ""); } } else if (name.startsWith("/content/users")) { Matcher m = Pattern.compile("/content/users/([^/]+)(/.*)?").matcher(name); if (m.matches()) { name = ServicesRegistry.getInstance().getJahiaUserManagerService() .getUserSplittingRule().getPathForUsername(m.group(1)); name = name + ((m.group(2) != null) ? m.group(2) : ""); } } else { name = pathMapping.get("/") + StringUtils.stripStart(name, "/"); } if (!zipentry.isDirectory()) { try { String filename = name.substring(name.lastIndexOf('/') + 1); ensureFile(session, name, zis, JCRContentUtils.getMimeType(filename), site); } catch (Exception e) { logger.error("Cannot upload file " + zipentry.getName(), e); } } else { ensureDir(session, name, site); } } } else if (name.equals(CATEGORIES_XML)) { catProps = importCategoriesAndGetUuidProps(zis, categoriesImportHandler); } else if (name.equals(DEFINITIONS_CND)) { reg = new NodeTypeRegistry(); // this is fishy: a new instance is created here when NodeTypeRegistry is meant to be used as a singleton try { for (Map.Entry<String, File> entry : NodeTypeRegistry.getSystemDefinitionsFiles() .entrySet()) { reg.addDefinitionsFile(entry.getValue(), entry.getKey()); } if (legacyImport) { JahiaCndReaderLegacy r = new JahiaCndReaderLegacy( new InputStreamReader(zis, Charsets.UTF_8), zipentry.getName(), file.getURL().getPath(), reg); r.parse(); } else { reg.addDefinitionsFile(new InputStreamResource(zis, zipentry.getName()), file.getURL().getPath()); } } catch (RepositoryException | ParseException e) { logger.error(e.getMessage(), e); } } else if (name.equals(DEFINITIONS_MAP)) { mapping = new DefinitionsMapping(); mapping.load(zis); } zis.closeEntry(); } } finally { closeInputStream(zis); } } // Import legacy content from 5.x and 6.x if (legacyImport) { long timerLegacy = System.currentTimeMillis(); final String originatingJahiaRelease = (String) infos.get("originatingJahiaRelease"); logger.info("Start legacy import, source version is " + originatingJahiaRelease); if (legacyMappingFilePath != null) { mapping = new DefinitionsMapping(); final InputStream fileInputStream = legacyMappingFilePath.getInputStream(); try { mapping.load(fileInputStream); } finally { IOUtils.closeQuietly(fileInputStream); } } if (legacyDefinitionsFilePath != null) { reg = new NodeTypeRegistry(); if ("6.1".equals(originatingJahiaRelease)) { logger.info("Loading the built in 6.1 definitions before processing the provided custom ones"); final List<String> builtInLegacyDefs = Arrays.asList("01-system-nodetypes.cnd", "02-jahiacore-nodetypes.cnd", "03-files-nodetypes.cnd", "04-jahiacontent-nodetypes.cnd", "05-standard-types.cnd", "10-extension-nodetypes.cnd", "11-preferences-nodetypes.cnd"); for (String builtInLegacyDefsFile : builtInLegacyDefs) { InputStreamReader inputStreamReader = null; try { final InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream( "org/jahia/migration/legacyDefinitions/jahia6/" + builtInLegacyDefsFile); if (inputStream != null) { inputStreamReader = new InputStreamReader(inputStream, "UTF-8"); final JahiaCndReaderLegacy r = new JahiaCndReaderLegacy(inputStreamReader, builtInLegacyDefsFile, file.getURL().getPath(), reg); r.parse(); } else { logger.error("Couldn't load " + builtInLegacyDefsFile); } } catch (ParseException e) { logger.error(e.getMessage(), e); } finally { IOUtils.closeQuietly(inputStreamReader); } } } else { try { for (Map.Entry<String, File> entry : NodeTypeRegistry.getSystemDefinitionsFiles() .entrySet()) { reg.addDefinitionsFile(entry.getValue(), entry.getKey()); } } catch (ParseException e) { logger.error("Cannot parse definitions : " + e.getMessage(), e); } } InputStreamReader streamReader = null; try { streamReader = new InputStreamReader(legacyDefinitionsFilePath.getInputStream(), "UTF-8"); JahiaCndReaderLegacy r = new JahiaCndReaderLegacy(streamReader, legacyDefinitionsFilePath.getFilename(), file.getURL().getPath(), reg); r.parse(); } catch (ParseException e) { logger.error(e.getMessage(), e); } finally { IOUtils.closeQuietly(streamReader); } } // Old import JCRNodeWrapper siteFolder = session.getNode("/sites/" + site.getSiteKey()); zis = new NoCloseZipInputStream(new BufferedInputStream(file.getInputStream())); try { int legacyImportHandlerCtnId = 1; while (true) { ZipEntry zipentry = zis.getNextEntry(); if (zipentry == null) break; String name = zipentry.getName(); if (name.equals(FILESACL_XML)) { logger.info("Importing file " + FILESACL_XML); importFilesAcl(site, file, zis, mapping, fileList); } else if (name.startsWith("export")) { logger.info("Importing file " + name); String languageCode; if (name.indexOf("_") != -1) { languageCode = name.substring(7, name.lastIndexOf(".")); } else { languageCode = site.getLanguagesAsLocales().iterator().next().toString(); } zipentry.getSize(); LegacyImportHandler importHandler = new LegacyImportHandler(session, siteFolder, reg, mapping, LanguageCodeConverters.languageCodeToLocale(languageCode), infos != null ? originatingJahiaRelease : null, legacyPidMappingTool, legacyImportHandlerCtnId); Map<String, List<String>> references = new LinkedHashMap<String, List<String>>(); importHandler.setReferences(references); InputStream documentInput = zis; if (this.xmlContentTransformers != null && this.xmlContentTransformers.size() > 0) { documentInput = new ZipInputStream(file.getInputStream()); while (!name.equals(((ZipInputStream) documentInput).getNextEntry().getName())) ; byte[] buffer = new byte[2048]; final File tmpDirectoryForSite = new File( new File(System.getProperty("java.io.tmpdir"), "jahia-migration"), FastDateFormat.getInstance("yyyy_MM_dd-HH_mm_ss_SSS").format(timerSite) + "_" + site.getSiteKey()); tmpDirectoryForSite.mkdirs(); File document = new File(tmpDirectoryForSite, "export_" + languageCode + "_00_extracted.xml"); final OutputStream output = new BufferedOutputStream(new FileOutputStream(document), 2048); int count = 0; while ((count = documentInput.read(buffer, 0, 2048)) > 0) { output.write(buffer, 0, count); } output.flush(); output.close(); documentInput.close(); for (XMLContentTransformer xct : xmlContentTransformers) { document = xct.transform(document, tmpDirectoryForSite); } documentInput = new FileInputStream(document); } handleImport(documentInput, importHandler, name); legacyImportHandlerCtnId = importHandler.getCtnId(); ReferencesHelper.resolveCrossReferences(session, references); siteFolder.getSession().save(JCRObservationManager.IMPORT); } zis.closeEntry(); } ReferencesHelper.resolveReferencesKeeper(session); siteFolder.getSession().save(JCRObservationManager.IMPORT); } finally { closeInputStream(zis); } logger.info("Done legacy import in {}", DateUtils.formatDurationWords(System.currentTimeMillis() - timerLegacy)); } categoriesImportHandler.setUuidProps(catProps); usersImportHandler.setUuidProps(userProps); // session.save(); session.save(JCRObservationManager.IMPORT); cleanFilesList(fileList); if (legacyImport && this.postImportPatcher != null) { final long timerPIP = System.currentTimeMillis(); logger.info("Executing post import patches"); this.postImportPatcher.executePatches(site); logger.info("Executed post import patches in {}", DateUtils.formatDurationWords(System.currentTimeMillis() - timerPIP)); } logger.info("Done importing site {} in {}", site != null ? site.getSiteKey() : "", DateUtils.formatDurationWords(System.currentTimeMillis() - timerSite)); }
From source file:com.taobao.android.builder.tools.sign.LocalSignedJarBuilder.java
/** * Copies the content of a Jar/Zip archive into the receiver archive. * <p/>An optional {@link IZipEntryFilter} allows to selectively choose which files * to copy over.//from w ww. ja v a 2s . com * * @param input the {@link InputStream} for the Jar/Zip to copy. * @param filter the filter or <code>null</code> * @throws IOException * @throws SignedJarBuilder.IZipEntryFilter.ZipAbortException if the {@link IZipEntryFilter} filter indicated that the write * must be aborted. */ public void writeZip(InputStream input, IZipEntryFilter filter) throws IOException, IZipEntryFilter.ZipAbortException { ZipInputStream zis = new ZipInputStream(input); try { // loop on the entries of the intermediary package and put them in the final package. ZipEntry entry; while ((entry = zis.getNextEntry()) != null) { String name = entry.getName(); // do not take directories or anything inside a potential META-INF folder. if (entry.isDirectory()) { continue; } // ignore some of the content in META-INF/ but not all if (name.startsWith("META-INF/")) { // ignore the manifest file. String subName = name.substring(9); if ("MANIFEST.MF".equals(subName)) { int count; ByteArrayOutputStream out = new ByteArrayOutputStream(); while ((count = zis.read(buffer)) != -1) { out.write(buffer, 0, count); } ByteArrayInputStream swapStream = new ByteArrayInputStream(out.toByteArray()); Manifest manifest = new Manifest(swapStream); mManifest.getMainAttributes().putAll(manifest.getMainAttributes()); continue; } // special case for Maven meta-data because we really don't care about them in apks. if (name.startsWith("META-INF/maven/")) { continue; } // check for subfolder int index = subName.indexOf('/'); if (index == -1) { // no sub folder, ignores signature files. if (subName.endsWith(".SF") || name.endsWith(".RSA") || name.endsWith(".DSA")) { continue; } } } // if we have a filter, we check the entry against it if (filter != null && !filter.checkEntry(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); } writeEntry(zis, newEntry); zis.closeEntry(); } } finally { zis.close(); } }
From source file:net.cbtltd.rest.interhome.A_Handler.java
/** * Get unzipped input stream for file name. * * @param fn the file name.//from ww w . ja v a 2 s .c om * @return the input stream. * @throws Throwable the exception that can be thrown. */ private final synchronized FileInputStream ftp(String fn) throws Throwable { URL url = new URL("ftp://ihxmlpartner:S13oPjEu@ftp.interhome.com/" + fn + ".zip;type=i"); URLConnection urlc = url.openConnection(); byte[] buf = new byte[1024]; ZipInputStream zinstream = new ZipInputStream(new BufferedInputStream(urlc.getInputStream())); ZipEntry zentry = zinstream.getNextEntry(); String entryName = zentry.getName(); FileOutputStream outstream = new FileOutputStream(entryName); int n; while ((n = zinstream.read(buf, 0, 1024)) > -1) { outstream.write(buf, 0, n); } outstream.close(); zinstream.closeEntry(); zinstream.close(); return new FileInputStream(entryName); }
From source file:usbong.android.utils.UsbongUtils.java
public static void unzip(String zipFile, String location) throws IOException { final int BUFFER_SIZE = 8192;//1024; //65536; int size;// w w w . jav a2s. co m byte[] buffer = new byte[BUFFER_SIZE]; ZipInputStream zin = new ZipInputStream(new BufferedInputStream(new FileInputStream(zipFile), BUFFER_SIZE)); try { Log.d(">>>>>>>", "1"); File f = new File(location); if (!f.exists()) { if (!f.isDirectory()) { f.mkdirs(); Log.d(">>>>>>>", "1.5: f.mkdirs()"); } } Log.d(">>>>>>>", "2"); // zin = new ZipInputStream(new BufferedInputStream(new FileInputStream(zipFile), BUFFER_SIZE)); try { ZipEntry ze = null; while ((ze = zin.getNextEntry()) != null) { String path = location + ze.getName(); Log.d(">>>>>>>", "3"); Log.d(">>>>>>>", "location: " + location); Log.d(">>>>>>>", "ze.getName(): " + ze.getName()); if (ze.isDirectory()) { Log.d(">>>>>>>", "4.1"); File unzipFile = new File(path); Log.d(">>>>>>>", "5.1"); Log.d(">>>>>>>", "path: " + path); if (!unzipFile.isDirectory()) { Log.d(">>>>>>>", "6.1"); unzipFile.mkdirs(); Log.d(">>>>>>>", "7.1"); } } else { Log.d(">>>>>>>", "4.2"); File file = new File(path); file.createNewFile(); FileOutputStream out = new FileOutputStream(path, false); BufferedOutputStream fout = new BufferedOutputStream(out, BUFFER_SIZE); try { while ((size = zin.read(buffer, 0, BUFFER_SIZE)) != -1) { Log.d(">>>>>>>", "4.3"); fout.write(buffer, 0, size); } Log.d(">>>>>>>", "4.4"); zin.closeEntry(); } finally { fout.flush(); fout.close(); } } } } catch (Exception e) { Log.e(TAG, "Unzip exception (Inner Exception)", e); } } catch (Exception e) { Log.e(TAG, "Unzip exception (Outer Exception)", e); } finally { zin.close(); } }
From source file:pl.psnc.synat.wrdz.zmd.object.parser.ObjectCreationParser.java
/** * Parses the request provided as ZIP.//w w w .ja va2s . c o m * * @param zis * input stream of the zip request * @param name * object name (can be <code>null</code>) * @return internal representation of the request * @throws IncompleteDataException * when some data are missing * @throws InvalidDataException * when some data are invalid */ public ObjectCreationRequest parse(ZipInputStream zis, String name) throws IncompleteDataException, InvalidDataException { ObjectCreationRequestBuilder requestBuilder = new ObjectCreationRequestBuilder(); if (!new File(root).mkdir()) { logger.error("Root folder " + root + " creation failed."); throw new WrdzRuntimeException("Root folder " + root + " creation failed."); } byte[] buffer = new byte[1024]; ZipEntry entry = null; try { while ((entry = zis.getNextEntry()) != null) { logger.debug("root: " + root + " 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; 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.addInputFileMetadataFile( metadataName.substring(0, metadataName.lastIndexOf('/')), ObjectManagerRequestHelper.makeUri(root, entry.getName())); } else { requestBuilder .addObjectMetadata(ObjectManagerRequestHelper.makeUri(root, entry.getName())); } } else { String contentName = entry.getName(); if (contentName.toLowerCase().startsWith("content/")) { contentName = contentName.substring(new String("content/").length()); } requestBuilder.setInputFileSource(contentName, ObjectManagerRequestHelper.makeUri(root, entry.getName())); requestBuilder.setInputFileDestination(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()); } requestBuilder.setName(name); return requestBuilder.build(); }
From source file:org.wso2.carbon.registry.extensions.handlers.ZipWSDLMediaTypeHandler.java
public void put(RequestContext requestContext) throws RegistryException { if (!CommonUtil.isUpdateLockAvailable()) { return;/*from w w w.j a va2 s .c om*/ } CommonUtil.acquireUpdateLock(); // setting up session local path map for mounted setup. boolean pathMapSet = setSessionLocalPathMap(requestContext); try { Resource resource = requestContext.getResource(); String path = requestContext.getResourcePath().getPath(); try { // If the WSDL is already there, we don't need to re-run this handler unless the content is changed. // Re-running this handler causes issues with downstream handlers and other behaviour (ex:- lifecycles). // If you need to do a replace programatically, delete-then-replace. if (requestContext.getRegistry().resourceExists(path)) { // TODO: Add logic to compare content, and return only if the content didn't change. return; } } catch (Exception ignore) { } 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; 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>(); List<String> wadlUriList = new LinkedList<String>(); List<String> swaggerUriList = 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); } 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); } xsdUriList.add(uri); } else if (entryName != null && entryName.toLowerCase().endsWith(wadlExtension)) { 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); } wadlUriList.add(uri); } else if (entryName != null && entryName.toLowerCase().endsWith(swaggerExtension)) { 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); } swaggerUriList.add(uri); } else if (entryName != null) { boolean isSkipFileExtension = false; for (String extension : skipFileExtensions) { if (entryName.toLowerCase().endsWith(extension.toLowerCase())) { isSkipFileExtension = true; break; } } if (!isSkipFileExtension) { 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() && wadlUriList.isEmpty() && uriList.isEmpty() && swaggerUriList.isEmpty()) { throw new RegistryException("No Files found in the given archive"); } for (String uri : wsdlUriList) { tasks.add(new UploadWSDLTask(requestContext, uri, CurrentSession.getTenantId(), CurrentSession.getUserRegistry(), CurrentSession.getUserRealm(), CurrentSession.getUser(), CurrentSession.getCallerTenantId(), localPathMap)); } for (String uri : xsdUriList) { tasks.add(new UploadXSDTask(requestContext, uri, CurrentSession.getTenantId(), CurrentSession.getUserRegistry(), CurrentSession.getUserRealm(), CurrentSession.getUser(), CurrentSession.getCallerTenantId(), localPathMap)); } for (String uri : wadlUriList) { tasks.add(new UploadWadlTask(requestContext, uri, CurrentSession.getTenantId(), CurrentSession.getUserRegistry(), CurrentSession.getUserRealm(), CurrentSession.getUser(), CurrentSession.getCallerTenantId(), localPathMap)); } for (String uri : swaggerUriList) { tasks.add(new UploadSwaggerTask(requestContext, uri, CurrentSession.getTenantId(), CurrentSession.getUserRegistry(), CurrentSession.getUserRealm(), CurrentSession.getUser(), CurrentSession.getCallerTenantId(), localPathMap)); } String mediaType = resource.getProperty("registry.mediaType"); if (mediaType != null) { for (String uri : uriList) { tasks.add(new UploadFileTask(requestContext, uri, CurrentSession.getTenantId(), CurrentSession.getUserRegistry(), CurrentSession.getUserRealm(), CurrentSession.getUser(), CurrentSession.getCallerTenantId(), localPathMap, mediaType)); } uriList.clear(); } // 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); } if (Transaction.isRollbacked()) { throw new RegistryException( "A nested transaction was rollbacked and therefore " + "cannot proceed with this action."); } requestContext.setProcessingComplete(true); } finally { CommonUtil.releaseUpdateLock(); removeSessionLocalPathMap(pathMapSet); } }
From source file:hoot.services.controllers.ingest.FileUploadResource.java
protected JSONObject _getZipContentType(final String zipFilePath, JSONArray contentTypes, String fName) throws Exception { JSONObject resultStat = new JSONObject(); String[] extList = { "gdb", "osm", "shp" }; int shpCnt = 0; int osmCnt = 0; int fgdbCnt = 0; ZipInputStream zis = new ZipInputStream(new FileInputStream(zipFilePath)); ZipEntry ze = zis.getNextEntry(); while (ze != null) { String zipName = ze.getName(); // check to see if zipName ends with slash and remove if (zipName.endsWith("/")) { zipName = zipName.substring(0, zipName.length() - 1); }//from ww w.j ava 2s. c o m String[] fileNameParts = zipName.split("\\."); String ext = null; int partsLen = fileNameParts.length; if (partsLen > 1) { ext = fileNameParts[partsLen - 1]; } //See if there is extension and if none then throw error if (ext == null) { throw new Exception("Unknown file type."); } else { // for each type of extensions for (int i = 0; i < extList.length; i++) { if (ext.equalsIgnoreCase(extList[i])) { if (ze.isDirectory()) { if (ext.equals("gdb")) { JSONObject contentType = new JSONObject(); contentType.put("type", "FGDB_ZIP"); contentType.put("name", fName + "/" + zipName); contentTypes.add(contentType); fgdbCnt++; } else { throw new Exception("Unknown folder type. Only gdb folder type is supported."); } } else //file { if (ext.equals("shp")) { JSONObject contentType = new JSONObject(); contentType.put("type", "OGR_ZIP"); contentType.put("name", fName + "/" + zipName); contentTypes.add(contentType); shpCnt++; } else if (ext.equals("osm")) { JSONObject contentType = new JSONObject(); contentType.put("type", "OSM_ZIP"); contentType.put("name", fName + "/" + zipName); contentTypes.add(contentType); osmCnt++; } else { // We will not throw error here since shape file can contain mutiple types of support files. // We will let hoot-core decide if it can handle the zip. } } } // We do not allow mix of ogr and osm in zip if ((shpCnt + fgdbCnt) > 0 && osmCnt > 0) { throw new Exception("Zip should not contain both osm and ogr types."); } } } ze = zis.getNextEntry(); } zis.closeEntry(); zis.close(); resultStat.put("shpcnt", shpCnt); resultStat.put("fgdbcnt", fgdbCnt); resultStat.put("osmcnt", osmCnt); return resultStat; }