Search in sources :

Example 11 with ZipException

use of net.lingala.zip4j.exception.ZipException in project v7files by thiloplanz.

the class ZipFile method index.

/**
 * index all individual files found in a zip archive already in storage
 *
 * @throws IOException
 */
public static final void index(ContentStorage storage, ContentPointer zipFile) throws IOException {
    Content zip = storage.getContent(zipFile);
    if (zip == null)
        throw new IllegalArgumentException("invalid ContentPointer " + zipFile);
    File tmp = File.createTempFile("v7files_zipfile_extractfile_", ".zip");
    try {
        OutputStream f = new FileOutputStream(tmp);
        IOUtils.copy(zip.getInputStream(), f);
        f.close();
        // open up the zip file
        HeaderReader r = new HeaderReader(new RandomAccessFile(tmp, "r"));
        ZipModel model = r.readAllHeaders();
        model.setZipFile(tmp.getAbsolutePath());
        Map<String, Object> map = zipFile.serialize();
        List<?> fhs = model.getCentralDirectory().getFileHeaders();
        for (Object _fh : fhs) {
            FileHeader fh = (FileHeader) _fh;
            UnzipEngine en = new UnzipEngine(model, fh);
            // this will read the local file header
            en.getInputStream();
            LocalFileHeader lh = en.getLocalFileHeader();
            store(storage, map, fh, lh);
        }
    } catch (ZipException e) {
        throw new IllegalArgumentException("ContentPointer does not refer to a zip file: " + zipFile, e);
    } finally {
        tmp.delete();
    }
}
Also used : OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) UnzipEngine(net.lingala.zip4j.unzip.UnzipEngine) ZipException(net.lingala.zip4j.exception.ZipException) LocalFileHeader(net.lingala.zip4j.model.LocalFileHeader) ZipModel(net.lingala.zip4j.model.ZipModel) RandomAccessFile(java.io.RandomAccessFile) InlineContent(v7db.files.spi.InlineContent) Content(v7db.files.spi.Content) FileOutputStream(java.io.FileOutputStream) HeaderReader(net.lingala.zip4j.core.HeaderReader) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File) LocalFileHeader(net.lingala.zip4j.model.LocalFileHeader) FileHeader(net.lingala.zip4j.model.FileHeader)

Example 12 with ZipException

use of net.lingala.zip4j.exception.ZipException in project v7files by thiloplanz.

the class ZipFile method extractFile.

/**
 * find the data indicated by the ContentPointer, treats it as a zip
 * archive, extracts the named file inside the archive, stores a reference
 * to it in the ContentStorage and returns a ContentPointer to it.
 *
 * @throws FileNotFoundException
 *             if the archive exists, but does not contain the named file
 * @throws IllegalArgumentException
 *             if the ContentPointer does not refer to a zip archive
 */
public static final ContentPointer extractFile(ContentStorage storage, ContentPointer zipFile, String fileName) throws IOException {
    Content zip = storage.getContent(zipFile);
    if (zip == null)
        throw new IllegalArgumentException("invalid ContentPointer " + zipFile);
    File tmp = File.createTempFile("v7files_zipfile_extractfile_", ".zip");
    try {
        OutputStream f = new FileOutputStream(tmp);
        IOUtils.copy(zip.getInputStream(), f);
        f.close();
        // open up the zip file
        HeaderReader r = new HeaderReader(new RandomAccessFile(tmp, "r"));
        ZipModel model = r.readAllHeaders();
        model.setZipFile(tmp.getAbsolutePath());
        List<?> fhs = model.getCentralDirectory().getFileHeaders();
        for (Object _fh : fhs) {
            FileHeader fh = (FileHeader) _fh;
            if (fileName.equals(fh.getFileName())) {
                UnzipEngine en = new UnzipEngine(model, fh);
                // this will read the local file header
                en.getInputStream();
                LocalFileHeader lh = en.getLocalFileHeader();
                return store(storage, zipFile.serialize(), fh, lh);
            }
        }
    } catch (ZipException e) {
        throw new IllegalArgumentException("ContentPointer does not refer to a zip file: " + zipFile, e);
    } finally {
        tmp.delete();
    }
    throw new FileNotFoundException("ContentPointer does not contain " + fileName + ": " + zipFile);
}
Also used : OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) UnzipEngine(net.lingala.zip4j.unzip.UnzipEngine) FileNotFoundException(java.io.FileNotFoundException) ZipException(net.lingala.zip4j.exception.ZipException) LocalFileHeader(net.lingala.zip4j.model.LocalFileHeader) ZipModel(net.lingala.zip4j.model.ZipModel) RandomAccessFile(java.io.RandomAccessFile) InlineContent(v7db.files.spi.InlineContent) Content(v7db.files.spi.Content) FileOutputStream(java.io.FileOutputStream) HeaderReader(net.lingala.zip4j.core.HeaderReader) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File) LocalFileHeader(net.lingala.zip4j.model.LocalFileHeader) FileHeader(net.lingala.zip4j.model.FileHeader)

Example 13 with ZipException

use of net.lingala.zip4j.exception.ZipException in project project-build-plugin by axonivy.

the class InstallEngineMojo method unpackEngine.

private void unpackEngine(File downloadZip) throws MojoExecutionException {
    try {
        String targetLocation = getRawEngineDirectory().getAbsolutePath();
        getLog().info("Unpacking engine " + downloadZip.getAbsolutePath() + " to " + targetLocation);
        ZipFile engineZip = new ZipFile(downloadZip);
        engineZip.extractAll(targetLocation);
    } catch (ZipException ex) {
        throw new MojoExecutionException("Failed to unpack downloaded engine '" + downloadZip + "'.", ex);
    }
}
Also used : ZipFile(net.lingala.zip4j.ZipFile) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) ZipException(net.lingala.zip4j.exception.ZipException)

Example 14 with ZipException

use of net.lingala.zip4j.exception.ZipException in project J2ME-Loader by nikita36078.

the class AppClassLoader method getResourceBytes.

private static byte[] getResourceBytes(String name) {
    if (zipFile == null) {
        final File file = new File(oldResDir, name);
        try {
            FileInputStream fis = new FileInputStream(file);
            byte[] data = IOUtils.toByteArray(fis);
            fis.close();
            return data;
        } catch (Exception e) {
            Log.w(TAG, "getResourceBytes: from file=" + file, e);
            return null;
        }
    }
    DataInputStream dis = null;
    try {
        FileHeader header = zipFile.getFileHeader(name);
        if (header == null) {
            return null;
        }
        dis = new DataInputStream(zipFile.getInputStream(header));
        byte[] data = new byte[(int) header.getUncompressedSize()];
        dis.readFully(data);
        return data;
    } catch (ZipException e) {
        Log.e(TAG, "getResourceBytes: ", e);
    } catch (IOException e) {
        Log.e(TAG, "getResourceBytes: ", e);
    } finally {
        if (dis != null) {
            try {
                dis.close();
            } catch (IOException e) {
                Log.e(TAG, "getResourceBytes: ", e);
            }
        }
    }
    return null;
}
Also used : ZipException(net.lingala.zip4j.exception.ZipException) IOException(java.io.IOException) DataInputStream(java.io.DataInputStream) File(java.io.File) ZipFile(net.lingala.zip4j.ZipFile) FileHeader(net.lingala.zip4j.model.FileHeader) FileInputStream(java.io.FileInputStream) IOException(java.io.IOException) ZipException(net.lingala.zip4j.exception.ZipException)

Example 15 with ZipException

use of net.lingala.zip4j.exception.ZipException in project ddf by codice.

the class ExportCommand method writeToZip.

private void writeToZip(/*Mutable,IO*/
ZipFile zipFile, Result result) {
    ZipParameters parameters = new ZipParameters();
    parameters.setSourceExternalStream(true);
    String id = result.getMetacard().getId();
    parameters.setFileNameInZip(Paths.get("metacards", id.substring(0, 3), id, "metacard", id + ".xml").toString());
    try {
        BinaryContent binaryMetacard = transformer.transform(result.getMetacard(), Collections.emptyMap());
        zipFile.addStream(binaryMetacard.getInputStream(), parameters);
    } catch (ZipException e) {
        LOGGER.error("Error processing result and adding to ZIP", e);
        throw new CatalogCommandRuntimeException(e);
    } catch (CatalogTransformerException e) {
        LOGGER.warn("Could not transform metacard. Metacard will not be added to zip [{}]", result.getMetacard().getId());
        console.printf("%sCould not transform metacard. Metacard will not be included in export. %s - %s%s%n", Ansi.ansi().fg(Ansi.Color.RED).toString(), result.getMetacard().getId(), result.getMetacard().getTitle(), Ansi.ansi().reset().toString());
    }
}
Also used : ZipException(net.lingala.zip4j.exception.ZipException) CatalogCommandRuntimeException(org.codice.ddf.commands.util.CatalogCommandRuntimeException) CatalogTransformerException(ddf.catalog.transform.CatalogTransformerException) BinaryContent(ddf.catalog.data.BinaryContent) ZipParameters(net.lingala.zip4j.model.ZipParameters)

Aggregations

ZipException (net.lingala.zip4j.exception.ZipException)23 ZipFile (net.lingala.zip4j.core.ZipFile)15 File (java.io.File)13 ZipParameters (net.lingala.zip4j.model.ZipParameters)9 FileHeader (net.lingala.zip4j.model.FileHeader)8 IOException (java.io.IOException)5 ArrayList (java.util.ArrayList)5 ZipFile (net.lingala.zip4j.ZipFile)3 WorldException (com.voxelgameslib.voxelgameslib.exception.WorldException)2 BinaryContent (ddf.catalog.data.BinaryContent)2 FileOutputStream (java.io.FileOutputStream)2 OutputStream (java.io.OutputStream)2 RandomAccessFile (java.io.RandomAccessFile)2 ZipFile (java.util.zip.ZipFile)2 HeaderReader (net.lingala.zip4j.core.HeaderReader)2 LocalFileHeader (net.lingala.zip4j.model.LocalFileHeader)2 ZipModel (net.lingala.zip4j.model.ZipModel)2 UnzipEngine (net.lingala.zip4j.unzip.UnzipEngine)2 FileMeta (org.molgenis.data.file.model.FileMeta)2 Content (v7db.files.spi.Content)2