org.apache.commons.compress.archivers.zip.ZipArchiveInputStream Java Examples

The following examples show how to use org.apache.commons.compress.archivers.zip.ZipArchiveInputStream. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: CompressExtension.java    From jphp with Apache License 2.0 6 votes vote down vote up
@Override
public void onRegister(CompileScope scope) {
    // register classes ...
    registerWrapperClass(scope, ArchiveEntry.class, PArchiveEntry.class);
    registerWrapperClass(scope, TarArchiveEntry.class, PTarArchiveEntry.class);
    registerWrapperClass(scope, ZipArchiveEntry.class, PZipArchiveEntry.class);

    registerWrapperClass(scope, ArchiveInputStream.class, PArchiveInput.class);
    registerWrapperClass(scope, TarArchiveInputStream.class, PTarArchiveInput.class);
    registerWrapperClass(scope, ZipArchiveInputStream.class, PZipArchiveInput.class);

    registerWrapperClass(scope, ArchiveOutputStream.class, PArchiveOutput.class);
    registerWrapperClass(scope, TarArchiveOutputStream.class, PTarArchiveOutput.class);
    registerWrapperClass(scope, ZipArchiveOutputStream.class, PZipArchiveOutput.class);

    registerClass(scope, PGzipOutputStream.class);
    registerClass(scope, PGzipInputStream.class);
    registerClass(scope, PBzip2OutputStream.class);
    registerClass(scope, PBZip2InputStream.class);
    registerClass(scope, PLz4OutputStream.class);
    registerClass(scope, PLz4InputStream.class);

    registerClass(scope, PArchive.class);
    registerClass(scope, PTarArchive.class);
    registerClass(scope, PZipArchive.class);
}
 
Example #2
Source File: IndexController.java    From config-toolkit with Apache License 2.0 6 votes vote down vote up
@PostMapping("/import/{version:.+}")
public ModelAndView importData(@PathVariable String version, MultipartFile file){
    final String fileName = file.getOriginalFilename();
    LOGGER.info("Upload file : {}", fileName);
    try (InputStream in = file.getInputStream()) {
        if (fileName.endsWith(PROPERTIES)) {
            saveGroup(version, fileName, in);

        } else if (fileName.endsWith(ZIP)) {
            try (ZipArchiveInputStream input = new ZipArchiveInputStream(in)) {
                ArchiveEntry nextEntry = null;
                while ((nextEntry = input.getNextEntry()) != null) {
                    String entryName = nextEntry.getName();
                    saveGroup(version, entryName, input);
                }
            }
        }
    } catch (IOException e) {
        LOGGER.error(e.getMessage(), e);
    }

    return new ModelAndView("redirect:/version/" + version);
}
 
Example #3
Source File: InstrumentTaskTest.java    From coroutines with GNU Lesser General Public License v3.0 6 votes vote down vote up
private Map<String, byte[]> readZipFromResource(String path) throws IOException {
    ClassLoader cl = ClassLoader.getSystemClassLoader();
    URL url = cl.getResource(path);
    Validate.isTrue(url != null);
    
    Map<String, byte[]> ret = new LinkedHashMap<>();
    
    try (InputStream is = url.openStream();
            ZipArchiveInputStream zais = new ZipArchiveInputStream(is)) {
        ZipArchiveEntry entry;
        while ((entry = zais.getNextZipEntry()) != null) {
            ret.put(entry.getName(), IOUtils.toByteArray(zais));
        }
    }
    
    return ret;
}
 
Example #4
Source File: TestUtils.java    From coroutines with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * Loads up a ZIP file that's contained in the classpath.
 * @param path path of ZIP resource
 * @return contents of files within the ZIP
 * @throws IOException if any IO error occurs
 * @throws NullPointerException if any argument is {@code null} or contains {@code null} elements
 * @throws IllegalArgumentException if {@code path} cannot be found, or if zipPaths contains duplicates
 */
public static Map<String, byte[]> readZipFromResource(String path) throws IOException {
    ClassLoader cl = ClassLoader.getSystemClassLoader();
    URL url = cl.getResource(path);
    Validate.isTrue(url != null);
    
    Map<String, byte[]> ret = new LinkedHashMap<>();
    
    try (InputStream is = url.openStream();
            ZipArchiveInputStream zais = new ZipArchiveInputStream(is)) {
        ZipArchiveEntry entry;
        while ((entry = zais.getNextZipEntry()) != null) {
            ret.put(entry.getName(), IOUtils.toByteArray(zais));
        }
    }
    
    return ret;
}
 
Example #5
Source File: TestInstrumentMojoTest.java    From coroutines with GNU Lesser General Public License v3.0 6 votes vote down vote up
private Map<String, byte[]> readZipFromResource(String path) throws IOException {
    ClassLoader cl = ClassLoader.getSystemClassLoader();
    URL url = cl.getResource(path);
    Validate.isTrue(url != null);
    
    Map<String, byte[]> ret = new LinkedHashMap<>();
    
    try (InputStream is = url.openStream();
            ZipArchiveInputStream zais = new ZipArchiveInputStream(is)) {
        ZipArchiveEntry entry;
        while ((entry = zais.getNextZipEntry()) != null) {
            ret.put(entry.getName(), IOUtils.toByteArray(zais));
        }
    }
    
    return ret;
}
 
Example #6
Source File: MainInstrumentMojoTest.java    From coroutines with GNU Lesser General Public License v3.0 6 votes vote down vote up
private Map<String, byte[]> readZipFromResource(String path) throws IOException {
    ClassLoader cl = ClassLoader.getSystemClassLoader();
    URL url = cl.getResource(path);
    Validate.isTrue(url != null);
    
    Map<String, byte[]> ret = new LinkedHashMap<>();
    
    try (InputStream is = url.openStream();
            ZipArchiveInputStream zais = new ZipArchiveInputStream(is)) {
        ZipArchiveEntry entry;
        while ((entry = zais.getNextZipEntry()) != null) {
            ret.put(entry.getName(), IOUtils.toByteArray(zais));
        }
    }
    
    return ret;
}
 
Example #7
Source File: CoroutinesAgentTest.java    From coroutines with GNU Lesser General Public License v3.0 6 votes vote down vote up
private Map<String, byte[]> readZipFromResource(String path) throws IOException {
    ClassLoader cl = ClassLoader.getSystemClassLoader();
    URL url = cl.getResource(path);
    Validate.isTrue(url != null);
    
    Map<String, byte[]> ret = new LinkedHashMap<>();
    
    try (InputStream is = url.openStream();
            ZipArchiveInputStream zais = new ZipArchiveInputStream(is)) {
        ZipArchiveEntry entry;
        while ((entry = zais.getNextZipEntry()) != null) {
            ret.put(entry.getName(), IOUtils.toByteArray(zais));
        }
    }
    
    return ret;
}
 
Example #8
Source File: GPXFileSystem.java    From tuxguitar with GNU Lesser General Public License v2.1 6 votes vote down vote up
public InputStream getFileContentsAsStream(String resource) throws Throwable {
	byte[] resourceBytes = null;

	ZipArchiveInputStream zipInputStream = new ZipArchiveInputStream(new ByteArrayInputStream(this.fsBuffer));
	ArchiveEntry zipEntry = null;
	while ((zipEntry = zipInputStream.getNextEntry()) != null) {
		if (zipEntry.getName().equals(resource)) {
			ByteArrayOutputStream out = new ByteArrayOutputStream();

			byte buffer[] = new byte[2048];
			int read = 0;
			while ((read = zipInputStream.read(buffer)) > 0) {
				out.write(buffer, 0, read);
			}

			resourceBytes = out.toByteArray();

			out.close();
		}
	}
	zipInputStream.close();

	return (resourceBytes != null ? new ByteArrayInputStream(resourceBytes) : null);
}
 
Example #9
Source File: EntityImportExport.java    From cuba with Apache License 2.0 6 votes vote down vote up
@Override
public Collection<Entity> importEntitiesFromZIP(byte[] zipBytes, EntityImportView view) {
    Collection<Entity> result = new ArrayList<>();
    Collection<? extends Entity> entities;
    ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(zipBytes);
    ZipArchiveInputStream archiveReader = new ZipArchiveInputStream(byteArrayInputStream);
    try {
        try {
            while (archiveReader.getNextZipEntry() != null) {
                String json = new String(readBytesFromEntry(archiveReader), StandardCharsets.UTF_8);
                entities = entitySerialization.entitiesCollectionFromJson(json,
                        null,
                        EntitySerializationOption.COMPACT_REPEATED_ENTITIES);
                result.addAll(importEntities(entities, view));
            }
        } catch (IOException e) {
            throw new RuntimeException("Exception occurred while importing report", e);
        }
    } finally {
        IOUtils.closeQuietly(archiveReader);
    }
    return result;
}
 
Example #10
Source File: FileUploadServlet.java    From EasyML with Apache License 2.0 6 votes vote down vote up
public static void unZipFiles(InputStream instream, String ID)
		throws IOException {
	ZipArchiveInputStream zin = new ZipArchiveInputStream(instream);
	java.util.zip.ZipEntry entry = null;
	while ((entry = zin.getNextZipEntry()) != null) {
		String zipEntryName = entry.getName();
		String outPath = zipEntryName.replaceAll("\\*", "/");
		String path = "lib";
		path += zipEntryName.substring(zipEntryName.indexOf('/'), zipEntryName.length());
		System.out.println("[path ]:" + path);
		if (!outPath.endsWith("/")) {
			InputStream in = zin;
			HDFSIO.uploadModel("/" + ID + "/" + path, in);
		}
	}
	zin.close();
}
 
Example #11
Source File: CMMDownloadTestUtil.java    From alfresco-repository with GNU Lesser General Public License v3.0 6 votes vote down vote up
public Set<String> getDownloadEntries(final NodeRef downloadNode)
{
    return transactionHelper.doInTransaction(new RetryingTransactionCallback<Set<String>>()
    {
        @Override
        public Set<String> execute() throws Throwable
        {
            Set<String> entryNames = new TreeSet<String>();
            ContentReader reader = contentService.getReader(downloadNode, ContentModel.PROP_CONTENT);
            try (ZipArchiveInputStream zipInputStream = new ZipArchiveInputStream(reader.getContentInputStream()))
            {
                ZipArchiveEntry zipEntry = null;
                while ((zipEntry = zipInputStream.getNextZipEntry()) != null)
                {
                    String name = zipEntry.getName();
                    entryNames.add(name);
                }
            }
            return entryNames;
        }
    });
}
 
Example #12
Source File: PressUtility.java    From jstarcraft-core with Apache License 2.0 6 votes vote down vote up
public static void decompressZip(File fromFile, File toDirectory) {
	try (FileInputStream fileInputStream = new FileInputStream(fromFile); ZipArchiveInputStream archiveInputStream = new ZipArchiveInputStream(fileInputStream)) {
		byte[] buffer = new byte[BUFFER_SIZE];
		ArchiveEntry archiveEntry;
		while (null != (archiveEntry = archiveInputStream.getNextEntry())) {
			File file = new File(toDirectory, archiveEntry.getName());
			try (FileOutputStream fileOutputStream = new FileOutputStream(file)) {
				int length = -1;
				while ((length = archiveInputStream.read(buffer)) != -1) {
					fileOutputStream.write(buffer, 0, length);
				}
				fileOutputStream.flush();
			}
		}
	} catch (IOException exception) {
		throw new IllegalStateException("解压ZIP异常:", exception);
	}
}
 
Example #13
Source File: ZipUtils.java    From youtubedl-android with GNU General Public License v3.0 6 votes vote down vote up
public static void unzip(InputStream inputStream, File targetDirectory) throws IOException, IllegalAccessException {
    try (ZipArchiveInputStream zis = new ZipArchiveInputStream(new BufferedInputStream(inputStream))) {
        ZipArchiveEntry entry = null;
        while ((entry = zis.getNextZipEntry()) != null) {
            File entryDestination = new File(targetDirectory, entry.getName());
            // prevent zipSlip
            if (!entryDestination.getCanonicalPath().startsWith(targetDirectory.getCanonicalPath() + File.separator)) {
                throw new IllegalAccessException("Entry is outside of the target dir: " + entry.getName());
            }
            if (entry.isDirectory()) {
                entryDestination.mkdirs();
            } else {
                entryDestination.getParentFile().mkdirs();
                try (OutputStream out = new FileOutputStream(entryDestination)) {
                    IOUtils.copy(zis, out);
                }
            }

        }
    }
}
 
Example #14
Source File: DownloadServiceIntegrationTest.java    From alfresco-repository with GNU Lesser General Public License v3.0 5 votes vote down vote up
private Set<String> getEntries(final NodeRef downloadNode)
{
    return TRANSACTION_HELPER.doInTransaction(new RetryingTransactionCallback<Set<String>>()
    {

        @Override
        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;
        }
    });
}
 
Example #15
Source File: CompressedApplicationInputStreamTest.java    From vespa with Apache License 2.0 5 votes vote down vote up
@Test
public void require_that_valid_zip_application_can_be_unpacked() throws IOException {
    File outFile = createZipFile();
    CompressedApplicationInputStream unpacked = CompressedApplicationInputStream.createFromCompressedStream(
            new ZipArchiveInputStream(new FileInputStream(outFile)));
    File outApp = unpacked.decompress();
    assertTestApp(outApp);
}
 
Example #16
Source File: EntityImportExport.java    From cuba with Apache License 2.0 5 votes vote down vote up
protected byte[] readBytesFromEntry(ZipArchiveInputStream archiveReader) throws IOException {
    return IOUtils.toByteArray(archiveReader);
}
 
Example #17
Source File: ZipStreamer.java    From secure-data-service with Apache License 2.0 5 votes vote down vote up
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);
   }

}
 
Example #18
Source File: ArchiveExtractor.java    From carbon-kernel with Apache License 2.0 5 votes vote down vote up
/**
 * Extract zip file in the system to a target Directory.
 *
 * @param path            path of the archive to be extract
 * @param targetDirectory where to extract to
 * @throws IOException on I/O error
 */
public static void extract(Path path, File targetDirectory) throws IOException {
    if (path.toString().endsWith("." + Constants.ZIP_EXTENSION)) {
        extract(new ZipArchiveInputStream(new FileInputStream(path.toFile())), targetDirectory);
    } else {
        throw new TestContainerException("Unknown packaging of distribution; only zip can be handled.");
    }
}
 
Example #19
Source File: P4ExtFileUtils.java    From p4ic4idea with Apache License 2.0 5 votes vote down vote up
public static void extractResource(@Nullable ClassLoader cl, @Nullable Object parentObject,
        @Nonnull String resourceLocation, @Nonnull File outputFile, boolean uncompress)
        throws IOException {
    // if (outputFile.exists()) {
    //     throw new IOException("Cannot overwrite existing file: " + outputFile);
    // }
    File parent = outputFile.getParentFile();
    if (parent != null && !parent.exists()) {
        if (!parent.mkdirs()) {
            throw new IOException("Could not create directory " + parent);
        }
    }
    InputStream inp = new BufferedInputStream(getStream(cl, parentObject, resourceLocation));
    if (uncompress) {
        if (resourceLocation.endsWith(".tar.bz2")) {
            extractArchive(new TarArchiveInputStream(new BZip2CompressorInputStream(inp)), outputFile);
            return;
        }
        if (resourceLocation.endsWith(".tar.xz")) {
            extractArchive(new TarArchiveInputStream(new XZCompressorInputStream(inp)), outputFile);
            return;
        }
        if (resourceLocation.endsWith(".tar.gz") || resourceLocation.endsWith(".tgz")) {
            extractArchive(new TarArchiveInputStream(new GzipCompressorInputStream(inp)), outputFile);
            return;
        }
        if (resourceLocation.endsWith(".tar")) {
            extractArchive(new TarArchiveInputStream(inp), outputFile);
            return;
        }
        if (resourceLocation.endsWith(".zip")) {
            extractArchive(new ZipArchiveInputStream(inp), outputFile);
            return;
        }
    }
    extractFile(inp, outputFile);
}
 
Example #20
Source File: ZipEdgeArchiveBuilder.java    From datacollector with Apache License 2.0 5 votes vote down vote up
@Override
public void finish() throws IOException {
  try (
      ZipArchiveOutputStream zipArchiveOutput = new ZipArchiveOutputStream(outputStream);
      ZipArchiveInputStream zipArchiveInput = new ZipArchiveInputStream(new FileInputStream(edgeArchive))
  ) {
    ZipArchiveEntry entry = zipArchiveInput.getNextZipEntry();

    while (entry != null) {
      zipArchiveOutput.putArchiveEntry(entry);
      IOUtils.copy(zipArchiveInput, zipArchiveOutput);
      zipArchiveOutput.closeArchiveEntry();
      entry = zipArchiveInput.getNextZipEntry();
    }

    for (PipelineConfigurationJson pipelineConfiguration : pipelineConfigurationList) {
      addArchiveEntry(zipArchiveOutput,
          pipelineConfiguration,
          pipelineConfiguration.getPipelineId(),
          PIPELINE_JSON_FILE
      );
      addArchiveEntry(zipArchiveOutput,
          pipelineConfiguration.getInfo(),
          pipelineConfiguration.getPipelineId(),
          PIPELINE_INFO_FILE
      );
    }

    zipArchiveOutput.finish();
  }
}
 
Example #21
Source File: ArtifactsXmlAbsoluteUrlRemoverTest.java    From nexus-repository-p2 with Eclipse Public License 1.0 5 votes vote down vote up
@Test
public void removeAbsoluteUrlFromJar() throws Exception {
  when(artifactsXml.get()).thenReturn(getClass().getResourceAsStream(ARTIFACTS_JAR));
  TempBlob modified = underTest.removeMirrorUrlFromArtifactsXml(artifactsXml, repository, "jar");
  try (ZipArchiveInputStream zip = new ZipArchiveInputStream(modified.get())) {
    zip.getNextZipEntry();
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    IOUtils.copy(zip, out);
    assertXmlMatches(new ByteArrayInputStream(out.toByteArray()), ARTIFACTS_XML_WITHOUT_ABSOLUTE);
  }
  catch (IOException e) {
    fail("Exception not expected. Is this file a JAR? " + e.getMessage());
  }
}
 
Example #22
Source File: UnpackContent.java    From nifi with Apache License 2.0 4 votes vote down vote up
@Override
public void unpack(final ProcessSession session, final FlowFile source, final List<FlowFile> unpacked) {
    final String fragmentId = UUID.randomUUID().toString();
    session.read(source, new InputStreamCallback() {
        @Override
        public void process(final InputStream in) throws IOException {
            int fragmentCount = 0;
            try (final ZipArchiveInputStream zipIn = new ZipArchiveInputStream(new BufferedInputStream(in))) {
                ArchiveEntry zipEntry;
                while ((zipEntry = zipIn.getNextEntry()) != null) {
                    if (zipEntry.isDirectory() || !fileMatches(zipEntry)) {
                        continue;
                    }
                    final File file = new File(zipEntry.getName());
                    final String parentDirectory = (file.getParent() == null) ? "/" : file.getParent();
                    final Path absPath = file.toPath().toAbsolutePath();
                    final String absPathString = absPath.getParent().toString() + "/";

                    FlowFile unpackedFile = session.create(source);
                    try {
                        final Map<String, String> attributes = new HashMap<>();
                        attributes.put(CoreAttributes.FILENAME.key(), file.getName());
                        attributes.put(CoreAttributes.PATH.key(), parentDirectory);
                        attributes.put(CoreAttributes.ABSOLUTE_PATH.key(), absPathString);
                        attributes.put(CoreAttributes.MIME_TYPE.key(), OCTET_STREAM);

                        attributes.put(FRAGMENT_ID, fragmentId);
                        attributes.put(FRAGMENT_INDEX, String.valueOf(++fragmentCount));

                        unpackedFile = session.putAllAttributes(unpackedFile, attributes);
                        unpackedFile = session.write(unpackedFile, new OutputStreamCallback() {
                            @Override
                            public void process(final OutputStream out) throws IOException {
                                StreamUtils.copy(zipIn, out);
                            }
                        });
                    } finally {
                        unpacked.add(unpackedFile);
                    }
                }
            }
        }
    });
}
 
Example #23
Source File: PZipArchive.java    From jphp with Apache License 2.0 4 votes vote down vote up
@Override
protected PArchiveInput createInput(Environment env) {
    return new PZipArchiveInput(env, new ZipArchiveInputStream(Stream.getInputStream(env, getSource())));
}
 
Example #24
Source File: PZipArchiveInput.java    From jphp with Apache License 2.0 4 votes vote down vote up
public PZipArchiveInput(Environment env, ZipArchiveInputStream wrappedObject) {
    super(env, wrappedObject);
}
 
Example #25
Source File: PZipArchiveInput.java    From jphp with Apache License 2.0 4 votes vote down vote up
@Signature
public void __construct(InputStream inputStream, String encoding) {
    __wrappedObject = new ZipArchiveInputStream(inputStream, encoding != null && encoding.isEmpty() ? null : encoding);
}
 
Example #26
Source File: ImportExportServiceImplTest.java    From webanno with Apache License 2.0 4 votes vote down vote up
@Test
public void thatExportContainsNoCasMetadata() throws Exception
{
    SourceDocument sd = makeSourceDocument(1l, 1l);

    // Create type system with built-in types, internal types, but without any project-specific
    // types.
    List<TypeSystemDescription> typeSystems = new ArrayList<>();
    typeSystems.add(createTypeSystemDescription());
    typeSystems.add(CasMetadataUtils.getInternalTypeSystem());
    TypeSystemDescription ts = mergeTypeSystems(typeSystems);
    
    // Prepare a test CAS with a CASMetadata annotation (DocumentMetaData is added as well
    // because the DKPro Core writers used by the ImportExportService expect it.
    JCas jcas = JCasFactory.createJCas(ts);
    casStorageSession.add("jcas", EXCLUSIVE_WRITE_ACCESS, jcas.getCas());
    jcas.setDocumentText("This is a test .");
    DocumentMetaData.create(jcas);
    CASMetadata cmd = new CASMetadata(jcas);
    cmd.addToIndexes(jcas);
    
    // Pass the CAS through the export mechanism. Write as XMI because that is one of the 
    // formats which best retains the information from the CAS and is nicely human-readable
    // if the test needs to be debugged.
    File exportedXmi = sut.exportCasToFile(jcas.getCas(), sd, "testfile",
            sut.getFormatById(XmiFormatSupport.ID).get(), true);

    // Read the XMI back from the ZIP that was created by the exporter. This is because XMI
    // files are always serialized as XMI file + type system file.
    JCas jcas2 = JCasFactory.createJCas(ts);
    casStorageSession.add("jcas2", EXCLUSIVE_WRITE_ACCESS, jcas.getCas());
    try (ZipArchiveInputStream zipInput = new ZipArchiveInputStream(
            new FileInputStream(exportedXmi))) {
        ZipArchiveEntry entry;
        while ((entry = zipInput.getNextZipEntry()) != null) {
            if (entry.getName().endsWith(".xmi")) {
                XmiCasDeserializer.deserialize(zipInput, jcas2.getCas());
                break;
            }
        }
    }
    finally {
        exportedXmi.delete();
    }
    
    List<CASMetadata> result = new ArrayList<>(select(jcas2, CASMetadata.class));
    assertThat(result).hasSize(0);
}
 
Example #27
Source File: ZipKit.java    From halo-docs with Apache License 2.0 4 votes vote down vote up
public static void unpack(InputStream in, File target) throws IOException {
    try (ZipArchiveInputStream zipArchiveInputStream = new ZipArchiveInputStream(in)) {
        unpack(zipArchiveInputStream, target);
    }
}
 
Example #28
Source File: AppPackage.java    From attic-apex-core with Apache License 2.0 4 votes vote down vote up
public static void extractToDirectory(File directory, File appPackageFile) throws IOException
{
  extractToDirectory(directory, new ZipArchiveInputStream(new FileInputStream(appPackageFile), "UTF-8", true, true));
}
 
Example #29
Source File: AppPackage.java    From attic-apex-core with Apache License 2.0 4 votes vote down vote up
/**
 * Creates an App Package object.
 *
 * If app directory is to be processed, there may be resource leak in the class loader. Only pass true for short-lived
 * applications
 *
 * If contentFolder is not null, it will try to create the contentFolder, file will be retained on disk after App Package is closed
 * If contentFolder is null, temp folder will be created and will be cleaned on close()
 *
 * @param input
 * @param contentFolder  the folder that the app package will be extracted to
 * @param processAppDirectory
 * @throws java.io.IOException
 */
public AppPackage(InputStream input, File contentFolder, boolean processAppDirectory) throws IOException
{
  try (final ZipArchiveInputStream zipArchiveInputStream =
      new ZipArchiveInputStream(input, "UTF8", true, true)) {

    if (contentFolder != null) {
      FileUtils.forceMkdir(contentFolder);
      cleanOnClose = false;
    } else {
      cleanOnClose = true;
      contentFolder = Files.createTempDirectory("dt-appPackage-").toFile();
    }
    directory = contentFolder;

    Manifest manifest = extractToDirectory(directory, zipArchiveInputStream);
    if (manifest == null) {
      throw new IOException("Not a valid app package. MANIFEST.MF is not present.");
    }
    Attributes attr = manifest.getMainAttributes();
    appPackageName = attr.getValue(ATTRIBUTE_DT_APP_PACKAGE_NAME);
    appPackageVersion = attr.getValue(ATTRIBUTE_DT_APP_PACKAGE_VERSION);
    appPackageGroupId = attr.getValue(ATTRIBUTE_DT_APP_PACKAGE_GROUP_ID);
    dtEngineVersion = attr.getValue(ATTRIBUTE_DT_ENGINE_VERSION);
    appPackageDisplayName = attr.getValue(ATTRIBUTE_DT_APP_PACKAGE_DISPLAY_NAME);
    appPackageDescription = attr.getValue(ATTRIBUTE_DT_APP_PACKAGE_DESCRIPTION);
    String classPathString = attr.getValue(ATTRIBUTE_CLASS_PATH);
    if (appPackageName == null || appPackageVersion == null || classPathString == null) {
      throw new IOException("Not a valid app package.  App Package Name or Version or Class-Path is missing from MANIFEST.MF");
    }
    classPath.addAll(Arrays.asList(StringUtils.split(classPathString, " ")));

    File confDirectory = new File(directory, "conf");
    if (confDirectory.exists()) {
      processConfDirectory(confDirectory);
    }
    resourcesDirectory = new File(directory, "resources");

    File propertiesXml = new File(directory, "META-INF/properties.xml");
    if (propertiesXml.exists()) {
      processPropertiesXml(propertiesXml, null);
    }

    if (processAppDirectory) {
      processAppDirectory(false);
    }
  }
}
 
Example #30
Source File: ZipKit.java    From httpdoc with Apache License 2.0 4 votes vote down vote up
public static void unpack(InputStream in, File target) throws IOException {
    try (ZipArchiveInputStream zipArchiveInputStream = new ZipArchiveInputStream(in)) {
        unpack(zipArchiveInputStream, target);
    }
}