Example usage for org.apache.commons.compress.archivers.zip ZipArchiveInputStream ZipArchiveInputStream

List of usage examples for org.apache.commons.compress.archivers.zip ZipArchiveInputStream ZipArchiveInputStream

Introduction

In this page you can find the example usage for org.apache.commons.compress.archivers.zip ZipArchiveInputStream ZipArchiveInputStream.

Prototype

public ZipArchiveInputStream(InputStream inputStream) 

Source Link

Usage

From source file:de.dfki.km.perspecting.obie.connection.RDFTripleParser.java

private static InputStream getStream(InputStream stream, MediaType mediatype) throws Exception {

    switch (mediatype) {
    case BZIP://  ww w  . j  ava  2 s  .  c  o m
        return new BZip2CompressorInputStream(stream);
    case GZIP:
        return new GzipCompressorInputStream(stream);
    case ZIP:
        return new ZipArchiveInputStream(stream);

    default:
        return stream;

    }
}

From source file:br.com.thiaguten.archive.ZipArchive.java

@Override
protected ArchiveInputStream createArchiveInputStream(InputStream inputStream) throws IOException {
    return new ZipArchiveInputStream(inputStream);
}

From source file:edu.unc.lib.dl.util.ZipFileUtil.java

/**
 * Unzip to the given directory, creating subdirectories as needed, and
 * ignoring empty directories. Uses apache zip tools until java utilies are
 * updated to support utf-8./*from ww w . ja v  a2 s  .c  om*/
 */
public static void unzip(InputStream is, File destDir) throws IOException {
    BufferedInputStream bis = new BufferedInputStream(is);
    try (ZipArchiveInputStream zis = new ZipArchiveInputStream(bis)) {
        ArchiveEntry entry = null;
        while ((entry = zis.getNextZipEntry()) != null) {
            if (!entry.isDirectory()) {
                File f = new File(destDir, entry.getName());

                if (!isFileInsideDirectory(f, destDir)) {
                    throw new IOException(
                            "Attempt to write to path outside of destination directory: " + entry.getName());
                }

                f.getParentFile().mkdirs();
                int count;
                byte data[] = new byte[8192];
                // write the files to the disk
                try (FileOutputStream fos = new FileOutputStream(f);
                        BufferedOutputStream dest = new BufferedOutputStream(fos, 8192)) {
                    while ((count = zis.read(data, 0, 8192)) != -1) {
                        dest.write(data, 0, count);
                    }
                }
            }
        }
    }
}

From source file:com.torchmind.upm.bundle.BasicResource.java

/**
 * {@inheritDoc}/*from   www  .jav a  2  s. com*/
 */
@Nonnull
@Override
public ArchiveInputStream createArchiveInputStream() throws IllegalStateException, IOException {
    switch (this.type) {
    case RAW:
        throw new IllegalStateException("Cannot convert RAW resource into archive");
    case JAR:
        return new JarArchiveInputStream(this.createInputStream());
    case TAR_ARCHIVE:
        return new TarArchiveInputStream(this.createInputStream());
    case TAR_GZ_ARCHIVE:
        return new TarArchiveInputStream(new GzipCompressorInputStream(this.createInputStream()));
    case TAR_XZ_ARCHIVE:
        return new TarArchiveInputStream(new XZCompressorInputStream(this.createInputStream()));
    case ZIP_ARCHIVE:
        return new ZipArchiveInputStream(this.createInputStream());
    }

    throw new UnsupportedOperationException("No such resource type: " + this.type);
}

From source file:com.google.gerrit.acceptance.ssh.UploadArchiveIT.java

@Test
public void zipFormat() throws Exception {
    PushOneCommit.Result r = createChange();
    String abbreviated = r.getCommit().abbreviate(8).name();
    String c = command(r, abbreviated);

    InputStream out = adminSshSession.exec2("git-upload-archive " + project.get(), argumentsToInputStream(c));

    // Wrap with PacketLineIn to read ACK bytes from output stream
    PacketLineIn in = new PacketLineIn(out);
    String tmp = in.readString();
    assertThat(tmp).isEqualTo("ACK");
    tmp = in.readString();/*from  ww  w.j  a  v  a2 s . co  m*/

    // Skip length (4 bytes) + 1 byte
    // to position the output stream to the raw zip stream
    byte[] buffer = new byte[5];
    IO.readFully(out, buffer, 0, 5);
    Set<String> entryNames = new TreeSet<>();
    try (ZipArchiveInputStream zip = new ZipArchiveInputStream(out)) {
        ZipArchiveEntry zipEntry = zip.getNextZipEntry();
        while (zipEntry != null) {
            String name = zipEntry.getName();
            entryNames.add(name);
            zipEntry = zip.getNextZipEntry();
        }
    }

    assertThat(entryNames.size()).isEqualTo(1);
    assertThat(Iterables.getOnlyElement(entryNames))
            .isEqualTo(String.format("%s/%s", abbreviated, PushOneCommit.FILE_NAME));
}

From source file:gzipper.algorithms.Zip.java

@Override
protected void extract(String path, String name) throws IOException {
    try (ZipArchiveInputStream zis = new ZipArchiveInputStream(
            new DeflateCompressorInputStream(new BufferedInputStream(new FileInputStream(path + name))))) {

        ArchiveEntry entry = zis.getNextEntry();

        /*create main folder of gzip archive*/
        File folder = new File(Settings._outputPath + name.substring(0, 7));
        if (!folder.exists()) {
            folder.mkdir();/* w  w  w  .ja  v  a 2s  . com*/
        }
        while (entry != null & _runFlag) {
            String entryName = entry.getName();
            /*check if entry contains a directory*/
            if (entryName.contains("/")) {
                File newFile;
                if (Settings._isUnix) { //check OS for correct file path
                    newFile = new File(folder.getAbsolutePath() + "/" + entryName);
                } else {
                    newFile = new File(folder.getAbsolutePath() + "\\" + entryName);
                }
                /*mkdirs also creates parent directories*/
                if (!newFile.getParentFile().exists()) {
                    newFile.getParentFile().mkdirs();
                }
            }

            String newFilePath;

            if (Settings._isUnix) { //check OS for correct file path
                newFilePath = folder.getAbsolutePath() + "/" + entryName;
            } else {
                newFilePath = folder.getAbsolutePath() + "\\" + entryName;
            }
            /*create new OutputStream and write bytes to file*/
            try (BufferedOutputStream buf = new BufferedOutputStream(new FileOutputStream(newFilePath))) {
                byte[] buffer = new byte[4096];
                int readBytes;
                while ((readBytes = zis.read(buffer)) != -1) {
                    buf.write(buffer, 0, readBytes);
                }
            }
            entry = zis.getNextEntry();
        }
    }
}

From source file:com.petpet.c3po.gatherer.FileExtractor.java

/**
 * Obtains an apache compress {@link ArchiveInputStream} to the given archive
 * file./*from   www  . j av a  2  s.  co  m*/
 * 
 * @param src
 *          the archive file.
 * @return the stream.
 */
private static ArchiveInputStream getStream(String src) {
    FileInputStream fis = null;
    ArchiveInputStream is = null;

    try {

        fis = new FileInputStream(src);

        if (src.endsWith(".zip")) {

            is = new ZipArchiveInputStream(fis);

        } else {

            boolean zip = src.endsWith(".tgz") || src.endsWith(".gz");
            InputStream imp = (zip) ? new GZIPInputStream(fis, BUFFER_SIZE)
                    : new BufferedInputStream(fis, BUFFER_SIZE);
            is = new TarArchiveInputStream(imp, BUFFER_SIZE);

        }

    } catch (IOException e) {
        LOG.warn("An error occurred while obtaining the stream to the archive '{}'. Error: {}", src,
                e.getMessage());
    }
    return is;
}

From source file:com.igormaznitsa.zxpoly.utils.ROMLoader.java

public static RomData getROMFrom(final String url) throws IOException {
    final URI uri;
    try {/*  ww w . j a v  a 2s  .c o  m*/
        uri = new URI(url);
    } catch (URISyntaxException ex) {
        throw new IOException("Error in URL '" + url + "\'", ex);
    }
    final String scheme = uri.getScheme();
    final String userInfo = uri.getUserInfo();
    final String name;
    final String password;
    if (userInfo != null) {
        final String[] splitted = userInfo.split("\\:");
        name = splitted[0];
        password = splitted[1];
    } else {
        name = null;
        password = null;
    }

    final byte[] loaded;
    if (scheme.startsWith("http")) {
        loaded = loadHTTPArchive(url);
    } else if (scheme.startsWith("ftp")) {
        loaded = loadFTPArchive(uri.getHost(), uri.getPath(), name, password);
    } else {
        throw new IllegalArgumentException("Unsupported scheme [" + scheme + ']');
    }

    final ZipArchiveInputStream in = new ZipArchiveInputStream(new ByteArrayInputStream(loaded));

    byte[] rom48 = null;
    byte[] rom128 = null;
    byte[] romTrDos = null;

    while (true) {
        final ZipArchiveEntry entry = in.getNextZipEntry();
        if (entry == null) {
            break;
        }

        if (entry.isDirectory()) {
            continue;
        }

        if (ROM_48.equalsIgnoreCase(entry.getName())) {
            final int size = (int) entry.getSize();
            if (size > 16384) {
                throw new IOException("ROM 48 has too big size");
            }
            rom48 = new byte[16384];
            IOUtils.readFully(in, rom48, 0, size);
        } else if (ROM_128TR.equalsIgnoreCase(entry.getName())) {
            final int size = (int) entry.getSize();
            if (size > 16384) {
                throw new IOException("ROM 128TR has too big size");
            }
            rom128 = new byte[16384];
            IOUtils.readFully(in, rom128, 0, size);
        } else if (ROM_TRDOS.equalsIgnoreCase(entry.getName())) {
            final int size = (int) entry.getSize();
            if (size > 16384) {
                throw new IOException("ROM TRDOS has too big size");
            }
            romTrDos = new byte[16384];
            IOUtils.readFully(in, romTrDos, 0, size);
        }
    }

    if (rom48 == null) {
        throw new IOException(ROM_48 + " not found");
    }
    if (rom128 == null) {
        throw new IOException(ROM_128TR + " not found");
    }
    if (romTrDos == null) {
        throw new IOException(ROM_TRDOS + " not found");
    }

    return new RomData(rom48, rom128, romTrDos);
}

From source file:abfab3d.io.input.STSReader.java

/**
 * Load a STS file into a grid.// w w w.  j ava 2  s . c  o m
 *
 * @param is The stream
 * @return
 * @throws java.io.IOException
 */
public TriangleMesh[] loadMeshes(InputStream is) throws IOException {
    ZipArchiveInputStream zis = null;
    TriangleMesh[] ret_val = null;

    try {
        zis = new ZipArchiveInputStream(is);

        ArchiveEntry entry = null;
        int cnt = 0;

        while ((entry = zis.getNextEntry()) != null) {
            // TODO: not sure we can depend on this being first
            if (entry.getName().equals("manifest.xml")) {
                mf = parseManifest(zis);

                ret_val = new TriangleMesh[mf.getParts().size()];
            } else {
                MeshReader reader = new MeshReader(zis, "", FilenameUtils.getExtension(entry.getName()));
                IndexedTriangleSetBuilder its = new IndexedTriangleSetBuilder();
                reader.getTriangles(its);

                // TODO: in this case we could return a less heavy triangle mesh struct?
                WingedEdgeTriangleMesh mesh = new WingedEdgeTriangleMesh(its.getVertices(), its.getFaces());
                ret_val[cnt++] = mesh;
            }
        }
    } finally {
        zis.close();
    }

    return ret_val;
}

From source file:com.fizzed.stork.deploy.Archive.java

static private ArchiveInputStream newArchiveInputStream(Path file) throws IOException {
    String name = file.getFileName().toString();

    if (name.endsWith(".tar.gz")) {
        return new TarArchiveInputStream(new GzipCompressorInputStream(Files.newInputStream(file), true));
    } else if (name.endsWith(".zip")) {
        return new ZipArchiveInputStream(Files.newInputStream(file));
    } else {//from  w ww  .  ja v  a  2  s  .  co  m
        throw new IOException("Unsupported archive file type (we support .tar.gz and .zip)");
    }
}