/** * 文件解压 * * @param file 压缩文件 * @param destDir 解压目录 */ public static void decompress(File file, File destDir, String encoding) { if (!file.exists()) { return; } InputStream is = null; ZipArchiveInputStream zis = null; try { is = new FileInputStream(file); zis = new ZipArchiveInputStream(new FileInputStream(file), encoding); ArchiveEntry archiveEntry = null; while ((archiveEntry = zis.getNextEntry()) != null) { CompressUtilsHelper.decompress(zis, archiveEntry, destDir); } } catch (Exception e) { throw new CommonsException( "Decompress " + file.getPath() + " to " + destDir.getPath() + " failed!", e); } finally { IOUtils.closeQuietly(zis); IOUtils.closeQuietly(is); } }
private void readDataFromZip() { FileInputStream fis = null; ZipArchiveInputStream zis = null; try { // Create input stream fis = new FileInputStream(this.filePath); zis = new ZipArchiveInputStream(new BufferedInputStream(fis)); ArchiveEntry entry; // Extract files while ((entry = zis.getNextEntry()) != null) { if (!entry.isDirectory() && this.innerFileName.equals(entry.getName())) { System.out.println( "LOCATED INNER FILE THAT SHOULD BE PROCESSED <" + entry.getName() + ">"); extractTo(zis, entry); } } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { IOUtils.closeQuietly(zis); IOUtils.closeQuietly(fis); } }
private void extract(final String inputFile, final String outputDir) throws IOException { FileInputStream fileInputStream = null; ZipArchiveInputStream zipArchiveInputStream = null; FileOutputStream fileOutputStream = null; try { Log.d(this.getClass().getName(), "Will extract " + inputFile + " to " + outputDir); byte[] buffer = new byte[8192]; fileInputStream = new FileInputStream(inputFile); // We use null as encoding. zipArchiveInputStream = new ZipArchiveInputStream(fileInputStream, null, true); ArchiveEntry entry; while ((entry = zipArchiveInputStream.getNextEntry()) != null) { Log.d(this.getClass().getName(), "Extracting entry " + entry.getName()); File file = new File(outputDir, entry.getName()); if (entry.isDirectory()) { file.mkdirs(); } else { file.getParentFile().mkdirs(); fileOutputStream = new FileOutputStream(file); int bytesRead; while ((bytesRead = zipArchiveInputStream.read(buffer, 0, buffer.length)) != -1) fileOutputStream.write(buffer, 0, bytesRead); fileOutputStream.close(); fileOutputStream = null; } } // Delete the zip file File zipFile = new File(inputFile); zipFile.delete(); } catch (Exception e) { Log.e("UnzipperTask", "Error unzipping file: " + inputFile + ", " + e); } finally { try { zipArchiveInputStream.close(); fileInputStream.close(); if (fileOutputStream != null) { fileOutputStream.close(); } } catch (NullPointerException ex) { Log.e(this.getClass().getName(), "Error closing the file streams.", ex); } catch (IOException ex) { Log.e(this.getClass().getName(), "Error closing the file streams.", ex); } } }
public void extractTo(ZipArchiveInputStream zis, ArchiveEntry entry) throws IOException { int count = 0; int curCount = 0; byte[] data = new byte[BUFFER]; while ((count = zis.read(data, 0, BUFFER)) != -1) { curCount += count; processInputData(entry.getName(), curCount); } }
@Test public void testUnmarshalEntireState() throws Exception { // First, create a state file to unmarshal // 1. It must be a zip archive; unmarshalling a non-archive package state file isn't supported // 2. We use the liveMarshallerMap; no mocks. // 3. We use a live ArchiveStreamFactory underTest.setArchive(true); underTest.setMarshallerMap(liveMarshallerMap); underTest.setArxStreamFactory(new ZipArchiveStreamFactory()); File tmp = File.createTempFile(this.getClass().getName() + "_UnmarshalEntireState", ".zip"); OutputStream out = new FileOutputStream(tmp); underTest.serialize(state, out); assertTrue(tmp.exists() && tmp.length() > 1); // Verify that we wrote out a zip file. final byte[] signature = new byte[12]; InputStream in = new BufferedInputStream(new FileInputStream(tmp)); in.mark(signature.length); int bytesRead = org.apache.commons.compress.utils.IOUtils.readFully(in, signature); assertTrue(ZipArchiveInputStream.matches(signature, bytesRead)); in.reset(); // Create a new instance of PackageState, and deserialize the zip archive created above, which // contains the // test objects from the {@link #state prepared instance} of PackageState PackageState state = new PackageState(); // a new instance of PackageState with no internal state underTest.deserialize(state, in); Map<StreamId, PropertyDescriptor> pds = SerializationAnnotationUtil.getStreamDescriptors(PackageState.class); assertTrue(pds.size() > 0); pds.keySet() .stream() .forEach( streamId -> { try { assertNotNull( "Expected non-null value for PackageState field '" + pds.get(streamId).getName() + "', " + "StreamId '" + streamId + "'", pds.get(streamId).getReadMethod().invoke(state)); } catch (IllegalAccessException | InvocationTargetException e) { fail(e.getMessage()); } }); IOUtils.closeQuietly(in); IOUtils.closeQuietly(out); FileUtils.deleteQuietly(tmp); }
@Override public List<XarEntry> getEntries(File xarFile) throws IOException { List<XarEntry> documents = null; FileInputStream fis = new FileInputStream(xarFile); ZipArchiveInputStream zis = new ZipArchiveInputStream(fis); try { for (ZipArchiveEntry zipEntry = zis.getNextZipEntry(); zipEntry != null; zipEntry = zis.getNextZipEntry()) { if (!zipEntry.isDirectory()) { try { XarPageLimitedHandler documentHandler = new XarPageLimitedHandler(this.componentManager); parseDocument(zis, documentHandler); if (documents == null) { documents = new ArrayList<XarEntry>(); } XarEntry xarEntry = documentHandler.getXarEntry(); xarEntry.setEntryName(zipEntry.getName()); documents.add(xarEntry); } catch (NotADocumentException e) { // Impossible to know that before parsing } catch (Exception e) { this.logger.error("Failed to parse document [" + zipEntry.getName() + "]", e); } } } } finally { zis.close(); } return documents != null ? documents : Collections.<XarEntry>emptyList(); }
/** * Load this package in memory from an InputStream. It may be installed later using {@link * #install(XWikiContext)}. * * @param file an InputStream of a zipped package file * @param context current XWikiContext * @return an empty string, useless. * @throws IOException while reading the ZipFile * @throws XWikiException when package content is broken * @since 2.3M2 */ public String Import(InputStream file, XWikiContext context) throws IOException, XWikiException { ZipArchiveInputStream zis; ArchiveEntry entry; Document description = null; try { zis = new ZipArchiveInputStream(file, XAR_FILENAME_ENCODING, false); List<XWikiDocument> docsToLoad = new LinkedList<XWikiDocument>(); /* * Loop 1: Cycle through the zip input stream and load out all of the documents, when we find the * package.xml file we put it aside to so that we only include documents which are in the file. */ while ((entry = zis.getNextEntry()) != null) { if (entry.isDirectory() || (entry.getName().indexOf("META-INF") != -1)) { // The entry is either a directory or is something inside of the META-INF dir. // (we use that directory to put meta data such as LICENSE/NOTICE files.) continue; } else if (entry.getName().compareTo(DefaultPackageFileName) == 0) { // The entry is the manifest (package.xml). Read this differently. description = fromXml(new CloseShieldInputStream(zis)); } else { XWikiDocument doc = null; try { doc = readFromXML(new CloseShieldInputStream(zis)); } catch (Throwable ex) { LOGGER.warn( "Failed to parse document [" + entry.getName() + "] from XML during import, thus it will not be installed. " + "The error was: " + ex.getMessage()); // It will be listed in the "failed documents" section after the import. addToErrors(entry.getName().replaceAll("/", "."), context); continue; } // Run all of the registered DocumentFilters on this document and // if no filters throw exceptions, add it to the list to import. try { this.filter(doc, context); docsToLoad.add(doc); } catch (ExcludeDocumentException e) { LOGGER.info("Skip the document '" + doc.getDocumentReference() + "'"); } } } // Make sure a manifest was included in the package... if (description == null) { throw new PackageException( XWikiException.ERROR_XWIKI_UNKNOWN, "Could not find the package definition"); } /* * Loop 2: Cycle through the list of documents and if they are in the manifest then add them, otherwise log * a warning and add them to the skipped list. */ for (XWikiDocument doc : docsToLoad) { if (documentExistInPackageFile(doc.getFullName(), doc.getLanguage(), description)) { this.add(doc, context); } else { LOGGER.warn( "document " + doc.getDocumentReference() + " does not exist in package definition." + " It will not be installed."); // It will be listed in the "skipped documents" section after the // import. addToSkipped(doc.getFullName(), context); } } updateFileInfos(description); } catch (DocumentException e) { throw new PackageException(XWikiException.ERROR_XWIKI_UNKNOWN, "Error when reading the XML"); } return ""; }
private XarMergeResult importXARToWiki( XarFile previousXarFile, InputStream xarInputStream, String wiki, PackageConfiguration configuration) throws IOException { XarMergeResult mergeResult = new XarMergeResult(); ZipArchiveInputStream zis = new ZipArchiveInputStream(xarInputStream); XWikiContext xcontext = getXWikiContext(); String currentWiki = xcontext.getDatabase(); try { xcontext.setDatabase(wiki); this.observation.notify(new XARImportingEvent(), null, xcontext); for (ArchiveEntry entry = zis.getNextEntry(); entry != null; entry = zis.getNextEntry()) { if (!entry.isDirectory()) { DocumentImporterHandler documentHandler = new DocumentImporterHandler(this, this.componentManager, wiki); try { documentHandler.setPreviousXarFile(previousXarFile); documentHandler.setConfiguration(configuration); parseDocument(zis, documentHandler); if (documentHandler.getMergeResult() != null) { mergeResult.addMergeResult(documentHandler.getMergeResult()); } if (configuration.isLogEnabled()) { this.logger.info( "Successfully imported document [{}] in language [{}]", documentHandler.getDocument().getDocumentReference(), documentHandler.getDocument().getRealLanguage()); } } catch (NotADocumentException e) { // Impossible to know that before parsing this.logger.debug("Entry [" + entry + "] is not a document", e); } catch (Exception e) { this.logger.error("Failed to parse document [" + entry.getName() + "]", e); if (configuration.isLogEnabled()) { this.logger.info( "Failed to import document [{}] in language [{}]", documentHandler.getDocument().getDocumentReference(), documentHandler.getDocument().getRealLanguage()); } } } } } finally { this.observation.notify(new XARImportedEvent(), null, xcontext); xcontext.setDatabase(currentWiki); } return mergeResult; }