List of usage examples for org.apache.commons.compress.archivers.zip ZipArchiveInputStream getNextZipEntry
public ZipArchiveEntry getNextZipEntry() throws IOException
From source file:com.fujitsu.dc.core.rs.cell.LogResource.java
private Response getLog(final String logCollection, String logFileName) { if (CURRENT_COLLECTION.equals(logCollection)) { File logFile = new File(logFileName); if (!logFile.isFile() || !logFile.canRead()) { // ????????????????SC_OK? return getEmptyResponse(); }/*from www .j ava2 s . c o m*/ try { final InputStream isInvariable = new FileInputStream(logFile); return createResponse(isInvariable); } catch (FileNotFoundException e) { throw DcCoreException.Dav.RESOURCE_NOT_FOUND; } } else { ZipArchiveInputStream zipArchiveInputStream = null; BufferedInputStream bis = null; String archiveLogFileName = logFileName + ".zip"; try { log.info("EventLog file path : " + archiveLogFileName); zipArchiveInputStream = new ZipArchiveInputStream(new FileInputStream(archiveLogFileName)); bis = new BufferedInputStream(zipArchiveInputStream); // ?entry?? // ??1??????????????? ZipArchiveEntry nextZipEntry = zipArchiveInputStream.getNextZipEntry(); if (nextZipEntry == null) { IOUtils.closeQuietly(bis); throw DcCoreException.Event.ARCHIVE_FILE_CANNOT_OPEN; } return createResponse(bis); } catch (FileNotFoundException e1) { // ??????404? throw DcCoreException.Dav.RESOURCE_NOT_FOUND; } catch (IOException e) { log.info("Failed to read archive entry : " + e.getMessage()); throw DcCoreException.Event.ARCHIVE_FILE_CANNOT_OPEN; } } }
From source file:io.personium.core.rs.cell.LogResource.java
private Response getLog(final String logCollection, String logFileName) { if (CURRENT_COLLECTION.equals(logCollection)) { File logFile = new File(logFileName); if (!logFile.isFile() || !logFile.canRead()) { // ????????????????SC_OK? return getEmptyResponse(); }/* w ww . j a va2s . c o m*/ try { final InputStream isInvariable = new FileInputStream(logFile); return createResponse(isInvariable); } catch (FileNotFoundException e) { throw PersoniumCoreException.Dav.RESOURCE_NOT_FOUND; } } else { ZipArchiveInputStream zipArchiveInputStream = null; BufferedInputStream bis = null; String archiveLogFileName = logFileName + ".zip"; try { log.info("EventLog file path : " + archiveLogFileName); zipArchiveInputStream = new ZipArchiveInputStream(new FileInputStream(archiveLogFileName)); bis = new BufferedInputStream(zipArchiveInputStream); // ?entry?? // ??1??????????????? ZipArchiveEntry nextZipEntry = zipArchiveInputStream.getNextZipEntry(); if (nextZipEntry == null) { IOUtils.closeQuietly(bis); throw PersoniumCoreException.Event.ARCHIVE_FILE_CANNOT_OPEN; } return createResponse(bis); } catch (FileNotFoundException e1) { // ??????404? throw PersoniumCoreException.Dav.RESOURCE_NOT_FOUND; } catch (IOException e) { log.info("Failed to read archive entry : " + e.getMessage()); throw PersoniumCoreException.Event.ARCHIVE_FILE_CANNOT_OPEN; } } }
From source file:ee.sk.digidoc.factory.SAXDigiDocFactory.java
/** * Reads in a DigiDoc file. One of fname or isSdoc must be given. * @param fname signed doc filename/*from w ww . j a v a 2 s .c om*/ * @param isSdoc opened stream with DigiDoc data * The user must open and close it. * @param errs list of errors to fill with parsing errors. If given * then attempt is made to continue parsing on errors and return them in this list. * If not given (null) then the first error found will be thrown. * @return signed document object if successfully parsed */ private SignedDoc readSignedDocOfType(String fname, InputStream isSdoc, boolean isBdoc, List errs) throws DigiDocException { // Use an instance of ourselves as the SAX event handler SAXDigiDocFactory handler = this; m_errs = errs; DigiDocVerifyFactory.initProvider(); SAXParserFactory factory = SAXParserFactory.newInstance(); if (m_logger.isDebugEnabled()) m_logger.debug("Start reading ddoc/bdoc " + ((fname != null) ? "from file: " + fname : "from stream") + " bdoc: " + isBdoc); if (fname == null && isSdoc == null) { throw new DigiDocException(DigiDocException.ERR_READ_FILE, "No input file", null); } if (fname != null) { File inFile = new File(fname); if (!inFile.canRead() || inFile.length() == 0) { throw new DigiDocException(DigiDocException.ERR_READ_FILE, "Empty or unreadable input file", null); } } ZipFile zf = null; ZipArchiveInputStream zis = null; ZipArchiveEntry ze = null; InputStream isEntry = null; File fTmp = null; try { factory.setFeature("http://xml.org/sax/features/external-general-entities", false); factory.setFeature("http://xml.org/sax/features/external-parameter-entities", false); if (isBdoc) { // bdoc parsing // must be a bdoc document ? m_doc = new SignedDoc(); m_doc.setVersion(SignedDoc.BDOC_VERSION_1_0); m_doc.setFormat(SignedDoc.FORMAT_BDOC); Enumeration eFiles = null; if (fname != null) { zf = new ZipFile(fname, "UTF-8"); eFiles = zf.getEntries(); } else if (isSdoc != null) { zis = new ZipArchiveInputStream(isSdoc, "UTF-8", true, true); } ArrayList lSigFnames = new ArrayList(); ArrayList lDataFnames = new ArrayList(); // read all entries boolean bHasMimetype = false, bManifest1 = false; int nFil = 0; while ((zf != null && eFiles.hasMoreElements()) || (zis != null && ((ze = zis.getNextZipEntry()) != null))) { nFil++; // read entry if (zf != null) { // ZipFile ze = (ZipArchiveEntry) eFiles.nextElement(); isEntry = zf.getInputStream(ze); } else { // ZipArchiveInputStream int n = 0, nTot = 0; if ((ze.getName().equals(FILE_MIMETYPE) || ze.getName().equals(FILE_MANIFEST) || (ze.getName().startsWith(FILE_SIGNATURES) && ze.getName().endsWith(".xml"))) || (nMaxBdocFilCached <= 0 || (ze.getSize() < nMaxBdocFilCached && ze.getSize() >= 0))) { ByteArrayOutputStream bos = new ByteArrayOutputStream(); byte[] data = new byte[2048]; while ((n = zis.read(data)) > 0) { bos.write(data, 0, n); nTot += n; } if (m_logger.isDebugEnabled()) m_logger.debug("Read: " + nTot + " bytes from zip"); data = bos.toByteArray(); bos = null; isEntry = new ByteArrayInputStream(data); } else { File fCacheDir = new File(ConfigManager.instance().getStringProperty( "DIGIDOC_DF_CACHE_DIR", System.getProperty("java.io.tmpdir"))); fTmp = File.createTempFile("bdoc-data", ".tmp", fCacheDir); FileOutputStream fos = new FileOutputStream(fTmp); byte[] data = new byte[2048]; while ((n = zis.read(data)) > 0) { fos.write(data, 0, n); nTot += n; } if (m_logger.isDebugEnabled()) m_logger.debug("Read: " + nTot + " bytes from zip to: " + fTmp.getAbsolutePath()); fos.close(); isEntry = new FileInputStream(fTmp); } } if (m_logger.isDebugEnabled()) m_logger.debug("Entry: " + ze.getName() + " nlen: " + ze.getName().length() + " size: " + ze.getSize() + " dir: " + ze.isDirectory() + " comp-size: " + ze.getCompressedSize()); // mimetype file if (ze.getName().equals(FILE_MIMETYPE)) { if (m_logger.isDebugEnabled()) m_logger.debug("Check mimetype!"); checkBdocMimetype(isEntry); bHasMimetype = true; m_doc.setComment(ze.getComment()); if (nFil != 1) { m_logger.error("mimetype file is " + nFil + " file but must be first"); handleError(new DigiDocException(DigiDocException.ERR_DIGIDOC_BADXML, "mimetype file is not first zip entry", null)); } } else if (ze.getName().equals(FILE_MANIFEST)) { // manifest.xml file if (m_logger.isDebugEnabled()) m_logger.debug("Read manifest"); if (!bManifest1 && isEntry != null) { bManifest1 = true; BdocManifestParser mfparser = new BdocManifestParser(m_doc); mfparser.readManifest(isEntry); } else { m_logger.error("Found multiple manifest.xml files!"); throw new DigiDocException(DigiDocException.ERR_MULTIPLE_MANIFEST_FILES, "Found multiple manifest.xml files!", null); } } else if (ze.getName().startsWith(FILE_SIGNATURES) && ze.getName().endsWith(".xml")) { // some signature m_fileName = ze.getName(); if (m_logger.isDebugEnabled()) m_logger.debug("Reading bdoc siganture: " + m_fileName); boolean bExists = false; for (int j = 0; j < lSigFnames.size(); j++) { String s1 = (String) lSigFnames.get(j); if (s1.equals(m_fileName)) bExists = true; } if (bExists) { m_logger.error("Duplicate signature filename: " + m_fileName); handleError(new DigiDocException(DigiDocException.ERR_DIGIDOC_BADXML, "Duplicate signature filename: " + m_fileName, null)); } else lSigFnames.add(m_fileName); SAXParser saxParser = factory.newSAXParser(); ByteArrayOutputStream bos = new ByteArrayOutputStream(); int n = 0; byte[] data = new byte[2048]; while ((n = isEntry.read(data)) > 0) bos.write(data, 0, n); data = bos.toByteArray(); bos = null; if (m_logger.isDebugEnabled()) m_logger.debug( "Parsing bdoc: " + m_fileName + " size: " + ((data != null) ? data.length : 0)); saxParser.parse(new SignatureInputStream(new ByteArrayInputStream(data)), this); if (m_logger.isDebugEnabled()) m_logger.debug("Parsed bdoc: " + m_fileName); Signature sig1 = m_doc.getLastSignature(); m_sigComment = ze.getComment(); if (sig1 != null) { sig1.setPath(m_fileName); sig1.setComment(ze.getComment()); } } else { // probably a data file if (m_logger.isDebugEnabled()) m_logger.debug("Read data file: " + ze.getName()); if (!ze.isDirectory()) { boolean bExists = false; for (int j = 0; j < lDataFnames.size(); j++) { String s1 = (String) lDataFnames.get(j); if (s1.equals(ze.getName())) bExists = true; } if (bExists) { m_logger.error("Duplicate datafile filename: " + ze.getName()); handleError(new DigiDocException(DigiDocException.ERR_DIGIDOC_BADXML, "Duplicate datafile filename: " + ze.getName(), null)); } else lDataFnames.add(ze.getName()); DataFile df = m_doc.findDataFileById(ze.getName()); if (df != null) { if (ze.getSize() > 0) df.setSize(ze.getSize()); df.setContentType(DataFile.CONTENT_BINARY); df.setFileName(ze.getName()); } else { df = new DataFile(ze.getName(), DataFile.CONTENT_BINARY, ze.getName(), "application/binary", m_doc); if (m_doc.getDataFiles() == null) m_doc.setDataFiles(new ArrayList()); m_doc.getDataFiles().add(df); //m_doc.addDataFile(df); // this does some intiailization work unnecessary here } // enable caching if requested if (isEntry != null) df.setOrCacheBodyAndCalcHashes(isEntry); df.setComment(ze.getComment()); df.setLastModDt(new Date(ze.getTime())); // fix mime type according to DataObjectFormat Signature sig1 = m_doc.getLastSignature(); if (sig1 != null) { Reference dRef = sig1.getSignedInfo().getReferenceForDataFile(df); if (dRef != null) { DataObjectFormat dof = sig1.getSignedInfo() .getDataObjectFormatForReference(dRef); if (dof != null) { df.setMimeType(dof.getMimeType()); } } } } } if (fTmp != null) { fTmp.delete(); fTmp = null; } } // while zip entries if (!bHasMimetype) { m_logger.error("No mimetype file"); handleError(new DigiDocException(DigiDocException.ERR_DIGIDOC_BADXML, "Not a BDOC format file! No mimetype file!", null)); } // if no signatures exist then copy mime-type from manifest.xml to DataFile -s if (m_doc.countSignatures() == 0) { for (int i = 0; i < m_doc.countDataFiles(); i++) { DataFile df = m_doc.getDataFile(i); if (m_doc.getManifest() != null) { for (int j = 0; j < m_doc.getManifest().getNumFileEntries(); j++) { ManifestFileEntry mfe = m_doc.getManifest().getFileEntry(j); if (mfe.getFullPath() != null && mfe.getFullPath().equals(df.getFileName())) { df.setMimeType(mfe.getMediaType()); } // if fullpath } // for } // if } // for i } } else { // ddoc parsing if (m_logger.isDebugEnabled()) m_logger.debug("Reading ddoc: " + fname + " file: " + m_fileName); m_fileName = fname; SAXParser saxParser = factory.newSAXParser(); if (fname != null) saxParser.parse(new SignatureInputStream(new FileInputStream(fname)), this); else if (isSdoc != null) saxParser.parse(isSdoc, this); } } catch (org.xml.sax.SAXParseException ex) { m_logger.error("SAX Error: " + ex); handleError(ex); } catch (Exception ex) { m_logger.error("Error reading3: " + ex); ex.printStackTrace(); /*if(ex instanceof DigiDocException){ DigiDocException dex = (DigiDocException)ex; m_logger.error("Dex: " + ex); if(dex.getNestedException() != null) { dex.getNestedException().printStackTrace(); m_logger.error("Trace: "); } }*/ handleError(ex); } finally { // cleanup try { if (isEntry != null) { isEntry.close(); isEntry = null; } if (zis != null) zis.close(); if (zf != null) zf.close(); if (fTmp != null) { fTmp.delete(); fTmp = null; } } catch (Exception ex) { m_logger.error("Error closing streams and files: " + ex); } } // compare Manifest and DataFiles boolean bErrList = (errs != null); if (errs == null) errs = new ArrayList(); boolean bOk = DigiDocVerifyFactory.verifyManifestEntries(m_doc, errs); if (m_doc == null) { m_logger.error("Error reading4: doc == null"); handleError(new DigiDocException(DigiDocException.ERR_DIGIDOC_BADXML, "This document is not in ddoc or bdoc format", null)); } if (!bErrList && errs.size() > 0) { // if error list was not used then we have to throw exception. So we will throw the first one since we can only do it once DigiDocException ex = (DigiDocException) errs.get(0); throw ex; } return m_doc; }
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); ////from w w w . ja v a2 s . c om 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:nz.co.kakariki.networkutils.reader.ExtractArchive.java
/** * Top unzip method.//from w ww.j a va2s . com */ protected static void unzip(File zipfile) throws FileNotFoundException { File path = zipfile.getParentFile(); try { ZipArchiveInputStream zais = new ZipArchiveInputStream(new FileInputStream(zipfile)); ZipArchiveEntry z1 = null; while ((z1 = zais.getNextZipEntry()) != null) { String fn = z1.getName(); if (fn.contains("/")) { fn = fn.substring(z1.getName().lastIndexOf("/")); } File f = new File(path + File.separator + fn); FileOutputStream fos = new FileOutputStream(f); BufferedOutputStream bos = new BufferedOutputStream(fos, BUFFER); int n = 0; byte[] content = new byte[BUFFER]; while (-1 != (n = zais.read(content))) { fos.write(content, 0, n); } bos.flush(); bos.close(); fos.close(); } zais.close(); zipfile.delete(); } catch (IOException ioe) { jlog.fatal("IO read error :: " + ioe); } }
From source file:org.alfresco.repo.content.transform.AppleIWorksContentTransformer.java
@Override protected void transformInternal(ContentReader reader, ContentWriter writer, TransformationOptions options) throws Exception { final String sourceMimetype = reader.getMimetype(); final String sourceExtension = getMimetypeService().getExtension(sourceMimetype); final String targetMimetype = writer.getMimetype(); if (log.isDebugEnabled()) { StringBuilder msg = new StringBuilder(); msg.append("Transforming from ").append(sourceMimetype).append(" to ").append(targetMimetype); log.debug(msg.toString());/*from ww w .ja v a 2s . c o m*/ } ZipArchiveInputStream iWorksZip = null; try { // iWorks files are zip files (at least in recent versions, iWork 09). // If it's not a zip file, the resultant ZipException will be caught as an IOException below. iWorksZip = new ZipArchiveInputStream(reader.getContentInputStream()); ZipArchiveEntry entry = null; boolean found = false; while (!found && (entry = iWorksZip.getNextZipEntry()) != null) { if (MimetypeMap.MIMETYPE_IMAGE_JPEG.equals(targetMimetype) && entry.getName().equals(QUICK_LOOK_THUMBNAIL_JPG)) { writer.putContent(iWorksZip); found = true; } else if (MimetypeMap.MIMETYPE_PDF.equals(targetMimetype) && entry.getName().equals(QUICK_LOOK_PREVIEW_PDF)) { writer.putContent(iWorksZip); found = true; } } if (!found) { throw new AlfrescoRuntimeException( "Unable to transform " + sourceExtension + " file to " + targetMimetype); } } catch (FileNotFoundException e1) { throw new AlfrescoRuntimeException("Unable to transform " + sourceExtension + " file.", e1); } catch (IOException e) { throw new AlfrescoRuntimeException("Unable to transform " + sourceExtension + " file.", e); } finally { if (iWorksZip != null) { iWorksZip.close(); } } }
From source file:org.alfresco.repo.download.DownloadServiceIntegrationTest.java
private Set<String> getEntries(final NodeRef downloadNode) { return TRANSACTION_HELPER.doInTransaction(new RetryingTransactionCallback<Set<String>>() { @Override// w w w .j a v a 2s .c o m public Set<String> execute() throws Throwable { Set<String> entryNames = new TreeSet<String>(); ContentReader reader = CONTENT_SERVICE.getReader(downloadNode, ContentModel.PROP_CONTENT); ZipArchiveInputStream zipInputStream = new ZipArchiveInputStream(reader.getContentInputStream()); try { ZipArchiveEntry zipEntry = zipInputStream.getNextZipEntry(); while (zipEntry != null) { String name = zipEntry.getName(); entryNames.add(name); zipEntry = zipInputStream.getNextZipEntry(); } } finally { zipInputStream.close(); } return entryNames; } }); }
From source file:org.apache.slider.common.tools.SliderUtils.java
public static InputStream getApplicationResourceInputStream(FileSystem fs, Path appPath, String entry) throws IOException { InputStream is = null;/*from w w w . j a v a2 s . co m*/ FSDataInputStream appStream = null; try { appStream = fs.open(appPath); ZipArchiveInputStream zis = new ZipArchiveInputStream(appStream); ZipArchiveEntry zipEntry; boolean done = false; while (!done && (zipEntry = zis.getNextZipEntry()) != null) { if (entry.equals(zipEntry.getName())) { int size = (int) zipEntry.getSize(); if (size != -1) { log.info("Reading {} of size {}", zipEntry.getName(), zipEntry.getSize()); byte[] content = new byte[size]; int offset = 0; while (offset < size) { offset += zis.read(content, offset, size - offset); } is = new ByteArrayInputStream(content); } else { log.debug("Size unknown. Reading {}", zipEntry.getName()); ByteArrayOutputStream baos = new ByteArrayOutputStream(); while (true) { int byteRead = zis.read(); if (byteRead == -1) { break; } baos.write(byteRead); } is = new ByteArrayInputStream(baos.toByteArray()); } done = true; } } } finally { IOUtils.closeStream(appStream); } return is; }
From source file:org.apache.tika.parser.iwork.IWorkPackageParser.java
public void parse(InputStream stream, ContentHandler handler, Metadata metadata, ParseContext context) throws IOException, SAXException, TikaException { ZipArchiveInputStream zip = new ZipArchiveInputStream(stream); ZipArchiveEntry entry = zip.getNextZipEntry(); while (entry != null) { if (!IWORK_CONTENT_ENTRIES.contains(entry.getName())) { entry = zip.getNextZipEntry(); continue; }// w w w .ja v a 2 s. c om InputStream entryStream = new BufferedInputStream(zip, 4096); entryStream.mark(4096); IWORKDocumentType type = IWORKDocumentType.detectType(entryStream); entryStream.reset(); if (type != null) { XHTMLContentHandler xhtml = new XHTMLContentHandler(handler, metadata); ContentHandler contentHandler; switch (type) { case KEYNOTE: contentHandler = new KeynoteContentHandler(xhtml, metadata); break; case NUMBERS: contentHandler = new NumbersContentHandler(xhtml, metadata); break; case PAGES: contentHandler = new PagesContentHandler(xhtml, metadata); break; case ENCRYPTED: // We can't do anything for the file right now contentHandler = null; break; default: throw new TikaException("Unhandled iWorks file " + type); } metadata.add(Metadata.CONTENT_TYPE, type.getType().toString()); xhtml.startDocument(); if (contentHandler != null) { context.getSAXParser().parse(new CloseShieldInputStream(entryStream), new OfflineContentHandler(contentHandler)); } xhtml.endDocument(); } entry = zip.getNextZipEntry(); } // Don't close the zip InputStream (TIKA-1117). }
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; }/*from w w w . 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()); } } }