List of usage examples for org.apache.commons.compress.archivers.zip ZipArchiveEntry isDirectory
public boolean isDirectory()
From source file:net.test.aliyun.z7.DownLoadTest.java
public void doWork() throws Throwable { //init task to DB System.out.println("do Task " + index + "\t from :" + this.newKey); //// ww w .j a v a 2s .c o m try { OSSObject ossObject = client.getObject("files-subtitle-format-zip", this.newKey); InputStream infromOSS = ossObject.getObjectContent(); ZipArchiveInputStream inStream = new ZipArchiveInputStream(infromOSS, "GBK"); ZipArchiveEntry entry = null; StringBuffer buffer = new StringBuffer(); while ((entry = inStream.getNextZipEntry()) != null) { if (entry.isDirectory()) continue; IOUtils.copy(inStream, new ByteArrayOutputStream()); buffer.append(entry.getName() + "\n"); } infromOSS.close(); // ObjectMetadata meta = ossObject.getObjectMetadata(); int res = jdbc.update("update `oss-subtitle` set files=?,size=?,lastTime=now() where oss_key =?", buffer.toString(), meta.getContentLength(), newKey); System.out.println(this.index + " - Validation ok. -> " + res); if (res == 0) { res = jdbc.update( "insert into `oss-subtitle` (oss_key,files,ori_name,size,lastTime,doWork) values (?,?,?,?,now(),0)", newKey, buffer.toString(), meta.getContentDisposition(), meta.getContentLength()); } // } catch (Throwable e) { e.printStackTrace(); // int res = jdbc.update("update `oss-subtitle` set files=null,lastTime=now() where oss_key =?", newKey); System.out.println("\t error dump to db -> " + res); } }
From source file:org.abysm.onionzip.ZipFileExtractHelper.java
void extractZipArchiveEntries() throws IOException { ZipFile zipFile = new ZipFile(zipFilename, encoding); try {/* w w w . jav a2 s.c o m*/ for (Enumeration<ZipArchiveEntry> zipArchiveEntryEnumeration = zipFile .getEntries(); zipArchiveEntryEnumeration.hasMoreElements();) { ZipArchiveEntry entry = zipArchiveEntryEnumeration.nextElement(); System.out.println(entry.getName()); if (entry.isDirectory()) { Path directory = Paths.get(entry.getName()); Files.createDirectories(directory); } else if (entry.isUnixSymlink()) { Path symlink = Paths.get(entry.getName()); Path parentDirectory = symlink.getParent(); Path target = Paths.get(zipFile.getUnixSymlink(entry)); if (parentDirectory != null) { Files.createDirectories(parentDirectory); } Files.createSymbolicLink(symlink, target); } else { Path file = Paths.get(entry.getName()); Path parentDirectory = file.getParent(); if (parentDirectory != null) { Files.createDirectories(parentDirectory); } InputStream contentInputStream = zipFile.getInputStream(entry); FileOutputStream extractedFileOutputStream = new FileOutputStream(entry.getName()); try { IOUtils.copy(contentInputStream, extractedFileOutputStream); } finally { IOUtils.closeQuietly(contentInputStream); IOUtils.closeQuietly(extractedFileOutputStream); } FileTime fileTime = FileTime.fromMillis(entry.getLastModifiedDate().getTime()); Files.setLastModifiedTime(file, fileTime); } } } finally { ZipFile.closeQuietly(zipFile); } }
From source file:org.alfresco.repo.action.executer.ImporterActionExecuter.java
/** * Extract the file and folder structure of a ZIP file into the specified directory * /*from ww w . j a v a 2 s . c o m*/ * @param archive The ZIP archive to extract * @param extractDir The directory to extract into */ public static void extractFile(ZipFile archive, String extractDir) { String fileName; String destFileName; byte[] buffer = new byte[BUFFER_SIZE]; extractDir = extractDir + File.separator; try { for (Enumeration e = archive.getEntries(); e.hasMoreElements();) { ZipArchiveEntry entry = (ZipArchiveEntry) e.nextElement(); if (!entry.isDirectory()) { fileName = entry.getName(); fileName = fileName.replace('/', File.separatorChar); if (fileName.startsWith("/") || fileName.indexOf(":" + File.separator) == 1 || fileName.contains(".." + File.separator)) { throw new AlfrescoRuntimeException(ARCHIVE_CONTAINS_SUSPICIOUS_PATHS_ERROR); } destFileName = extractDir + fileName; File destFile = new File(destFileName); String parent = destFile.getParent(); if (parent != null) { File parentFile = new File(parent); if (!parentFile.exists()) parentFile.mkdirs(); } InputStream in = new BufferedInputStream(archive.getInputStream(entry), BUFFER_SIZE); OutputStream out = new BufferedOutputStream(new FileOutputStream(destFileName), BUFFER_SIZE); int count; while ((count = in.read(buffer)) != -1) { out.write(buffer, 0, count); } in.close(); out.close(); } else { File newdir = new File(extractDir + entry.getName()); newdir.mkdirs(); } } } catch (ZipException e) { throw new AlfrescoRuntimeException("Failed to process ZIP file.", e); } catch (FileNotFoundException e) { throw new AlfrescoRuntimeException("Failed to process ZIP file.", e); } catch (IOException e) { throw new AlfrescoRuntimeException("Failed to process ZIP file.", e); } }
From source file:org.alfresco.repo.importer.ACPImportPackageHandler.java
public Reader getDataStream() { try {//from w w w . j a v a 2s .c o m // find xml meta-data file ZipArchiveEntry xmlMetaDataEntry = null; // TODO: First, locate xml meta-data file by name // Scan the zip entries one by one (the slow approach) Enumeration entries = zipFile.getEntries(); while (entries.hasMoreElements()) { ZipArchiveEntry entry = (ZipArchiveEntry) entries.nextElement(); if (!entry.isDirectory()) { // Locate xml file in root of .acp String entryName = entry.getName(); if (entryName.endsWith(".xml") && entryName.indexOf('/') == -1 && entryName.indexOf('\\') == -1) { if (xmlMetaDataEntry != null) { throw new ImporterException( "Failed to find unique xml meta-data file within .acp package - multiple xml meta-data files exist."); } xmlMetaDataEntry = entry; } } } // oh dear, there's no data file if (xmlMetaDataEntry == null) { throw new ImporterException("Failed to find xml meta-data file within .acp package"); } // open the meta-data xml file InputStream dataStream = zipFile.getInputStream(xmlMetaDataEntry); Reader inputReader = (dataFileEncoding == null) ? new InputStreamReader(dataStream, DEFAULT_ENCODING) : new InputStreamReader(dataStream, dataFileEncoding); return new BufferedReader(inputReader); } catch (UnsupportedEncodingException e) { throw new ImporterException("Encoding " + dataFileEncoding + " is not supported"); } catch (IOException e) { throw new ImporterException( "Failed to open xml meta-data file within .acp package due to " + e.getMessage()); } }
From source file:org.apache.ant.compress.resources.ZipScanner.java
/** * Fills the file and directory maps with resources read from the * archive./*w w w . ja v a 2 s . c o m*/ * * @param src the archive to scan. * @param encoding encoding used to encode file names inside the archive. * @param fileEntries Map (name to resource) of non-directory * resources found inside the archive. * @param matchFileEntries Map (name to resource) of non-directory * resources found inside the archive that matched all include * patterns and didn't match any exclude patterns. * @param dirEntries Map (name to resource) of directory * resources found inside the archive. * @param matchDirEntries Map (name to resource) of directory * resources found inside the archive that matched all include * patterns and didn't match any exclude patterns. */ protected void fillMapsFromArchive(Resource src, String encoding, Map fileEntries, Map matchFileEntries, Map dirEntries, Map matchDirEntries) { FileProvider fp = (FileProvider) src.as(FileProvider.class); if (fp == null) { super.fillMapsFromArchive(src, encoding, fileEntries, matchFileEntries, dirEntries, matchDirEntries); return; } File srcFile = fp.getFile(); ZipArchiveEntry entry = null; ZipFile zf = null; try { try { zf = new ZipFile(srcFile, encoding); } catch (ZipException ex) { throw new BuildException("Problem reading " + srcFile, ex); } catch (IOException ex) { throw new BuildException("Problem opening " + srcFile, ex); } Enumeration e = zf.getEntries(); while (e.hasMoreElements()) { entry = (ZipArchiveEntry) e.nextElement(); if (getSkipUnreadableEntries() && !zf.canReadEntryData(entry)) { log(Messages.skippedIsUnreadable(entry)); continue; } Resource r = new ZipResource(srcFile, encoding, entry); String name = entry.getName(); if (entry.isDirectory()) { name = trimSeparator(name); dirEntries.put(name, r); if (match(name)) { matchDirEntries.put(name, r); } } else { fileEntries.put(name, r); if (match(name)) { matchFileEntries.put(name, r); } } } } finally { ZipFile.closeQuietly(zf); } }
From source file:org.apache.ant.compress.taskdefs.Unzip.java
protected void expandFile(FileUtils fileUtils, File srcF, File dir) { log("Expanding: " + srcF + " into " + dir, Project.MSG_INFO); ZipFile zf = null;/*from www . j a v a 2 s . co m*/ FileNameMapper mapper = getMapper(); if (!srcF.exists()) { throw new BuildException("Unable to expand " + srcF + " as the file does not exist", getLocation()); } try { zf = new ZipFile(srcF, getEncoding(), true); boolean empty = true; Enumeration e = zf.getEntries(); while (e.hasMoreElements()) { empty = false; ZipArchiveEntry ze = (ZipArchiveEntry) e.nextElement(); if (getSkipUnreadableEntries() && !zf.canReadEntryData(ze)) { log(Messages.skippedIsUnreadable(ze)); continue; } log("extracting " + ze.getName(), Project.MSG_DEBUG); InputStream is = null; try { extractFile(fileUtils, srcF, dir, is = zf.getInputStream(ze), ze.getName(), new Date(ze.getTime()), ze.isDirectory(), mapper); } finally { FileUtils.close(is); } } if (empty && getFailOnEmptyArchive()) { throw new BuildException("archive '" + srcF + "' is empty"); } log("expand complete", Project.MSG_VERBOSE); } catch (IOException ioe) { throw new BuildException("Error while expanding " + srcF.getPath() + "\n" + ioe.toString(), ioe); } finally { ZipFile.closeQuietly(zf); } }
From source file:org.apache.karaf.decanter.kibana6.KibanaController.java
public void download() throws Exception { File target = new File(workingDirectory, KIBANA_FOLDER); if (target.exists()) { LOGGER.warn("Kibana folder already exists, download is skipped"); return;/*from ww w .j a va 2s. c o m*/ } LOGGER.debug("Downloading Kibana from {}", KIBANA_LOCATION); if (isWindows()) { try (ZipArchiveInputStream inputStream = new ZipArchiveInputStream( new URL(KIBANA_LOCATION).openStream())) { ZipArchiveEntry entry; while ((entry = (ZipArchiveEntry) inputStream.getNextEntry()) != null) { File file = new File(workingDirectory, entry.getName()); if (entry.isDirectory()) { file.mkdirs(); } else { int read; byte[] buffer = new byte[4096]; try (FileOutputStream outputStream = new FileOutputStream(file)) { while ((read = inputStream.read(buffer, 0, 4096)) != -1) { outputStream.write(buffer, 0, read); } } } } } } else { try (GzipCompressorInputStream gzInputStream = new GzipCompressorInputStream( new URL(KIBANA_LOCATION).openStream())) { try (TarArchiveInputStream inputStream = new TarArchiveInputStream(gzInputStream)) { TarArchiveEntry entry; while ((entry = (TarArchiveEntry) inputStream.getNextEntry()) != null) { File file = new File(workingDirectory, entry.getName()); if (entry.isDirectory()) { file.mkdirs(); } else { int read; byte[] buffer = new byte[4096]; try (FileOutputStream outputStream = new FileOutputStream(file)) { while ((read = inputStream.read(buffer, 0, 4096)) != -1) { outputStream.write(buffer, 0, read); } } file.setLastModified(entry.getLastModifiedDate().getTime()); if (entry instanceof TarArchiveEntry) { int mode = ((TarArchiveEntry) entry).getMode(); if ((mode & 00100) > 0) { file.setExecutable(true, (mode & 00001) == 0); } } } } } } } overrideConfig(); }
From source file:org.apache.sis.internal.maven.Assembler.java
/** * Adds the given file in the ZIP file. If the given file is a directory, then this method * recursively adds all files contained in this directory. This method is invoked for zipping * the "application/sis-console/src/main/artifact" directory and sub-directories before to zip * the Pack200 file.//from w w w. j a v a 2s . c om */ private void appendRecursively(final File file, String relativeFile, final ZipArchiveOutputStream out, final byte[] buffer) throws IOException { if (file.isDirectory()) { relativeFile += '/'; } final ZipArchiveEntry entry = new ZipArchiveEntry(file, relativeFile); if (file.canExecute()) { entry.setUnixMode(0744); } out.putArchiveEntry(entry); if (!entry.isDirectory()) { final FileInputStream in = new FileInputStream(file); try { int n; while ((n = in.read(buffer)) >= 0) { out.write(buffer, 0, n); } } finally { in.close(); } } out.closeArchiveEntry(); if (entry.isDirectory()) { for (final String filename : file.list(this)) { appendRecursively(new File(file, filename), relativeFile.concat(filename), out, buffer); } } }
From source file:org.apache.tika.parser.microsoft.ooxml.TruncatedOOXMLTest.java
@Test @Ignore("for dev/debugging only") public void listStreams() throws Exception { File tstDir = new File(TruncatedOOXMLTest.class.getResource("/test-documents").toURI()); for (File f : tstDir.listFiles()) { if (f.isDirectory()) { continue; }// w ww. ja v a 2s .c o m if (f.getName().endsWith(".xlsx")) {// || f.getName().endsWith(".pptx") || f.getName().endsWith(".docx")) { } else { continue; } try (InputStream is = new FileInputStream(f)) { ZipArchiveInputStream zipArchiveInputStream = new ZipArchiveInputStream(is); ZipArchiveEntry zae = zipArchiveInputStream.getNextZipEntry(); int cnt = 0; while (zae != null && !zae.isDirectory() && ++cnt <= 10) { System.out.println(f.getName() + " : " + zae.getName()); if (zae.getName().equals("_rels/.rels")) { ByteArrayOutputStream bos = new ByteArrayOutputStream(); IOUtils.copy(zipArchiveInputStream, bos); System.out.println(new String(bos.toByteArray(), StandardCharsets.UTF_8)); } zae = zipArchiveInputStream.getNextZipEntry(); } } catch (Exception e) { System.out.println(f.getName() + " : " + e.getMessage()); } } }
From source file:org.apache.tika.parser.pkg.StreamingZipContainerDetector.java
/** * * @param is inputstream to read from. Callers must mark/reset the stream * before/after this call to detect. This call does not close the stream! * Depending on the file type, this call to detect may read the entire stream. * Make sure to use a {@link org.apache.tika.io.BoundedInputStream} or similar * if you want to protect against reading the entire stream. * @return/* w w w . j a v a 2 s .co m*/ */ static MediaType detect(InputStream is) { Set<String> fileNames = new HashSet<>(); Set<String> directoryNames = new HashSet<>(); try (ZipArchiveInputStream zipArchiveInputStream = new ZipArchiveInputStream( new CloseShieldInputStream(is))) { ZipArchiveEntry zae = zipArchiveInputStream.getNextZipEntry(); while (zae != null) { String name = zae.getName(); if (zae.isDirectory()) { directoryNames.add(name); zae = zipArchiveInputStream.getNextZipEntry(); continue; } fileNames.add(name); //we could also parse _rel/.rels, but if // there isn't a valid content_types, then POI //will throw an exception...Better to backoff to PKG //than correctly identify a truncated if (name.equals("[Content_Types].xml")) { MediaType mt = parseOOXMLContentTypes(zipArchiveInputStream); if (mt != null) { return mt; } return TIKA_OOXML; } else if (IWorkPackageParser.IWORK_CONTENT_ENTRIES.contains(name)) { IWorkPackageParser.IWORKDocumentType type = IWorkPackageParser.IWORKDocumentType .detectType(zipArchiveInputStream); if (type != null) { return type.getType(); } } else if (name.equals("mimetype")) { //odt -- TODO -- bound the read and check that the results are //valid return MediaType.parse(IOUtils.toString(zipArchiveInputStream, UTF_8)); } zae = zipArchiveInputStream.getNextZipEntry(); } } catch (SecurityException e) { throw e; } catch (Exception e) { //swallow } //entrynames is the union of directory names and file names Set<String> entryNames = new HashSet<>(fileNames); entryNames.addAll(fileNames); MediaType mt = detectKmz(fileNames); if (mt != null) { return mt; } mt = detectJar(entryNames); if (mt != null) { return mt; } mt = detectIpa(entryNames); if (mt != null) { return mt; } mt = detectIWorks(entryNames); if (mt != null) { return mt; } int hits = 0; for (String s : OOXML_HINTS) { if (entryNames.contains(s)) { if (++hits > 2) { return TIKA_OOXML; } } } return MediaType.APPLICATION_ZIP; }