Example usage for org.apache.commons.compress.archivers ArchiveStreamFactory ZIP

List of usage examples for org.apache.commons.compress.archivers ArchiveStreamFactory ZIP

Introduction

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

Prototype

String ZIP

To view the source code for org.apache.commons.compress.archivers ArchiveStreamFactory ZIP.

Click Source Link

Document

Constant used to identify the ZIP archive format.

Usage

From source file:org.dataconservancy.packaging.tool.impl.generator.BagItPackageAssemblerTest.java

/**
 * Test assembling a bag which contain a data file and a metadata file with ZIP format
 * @throws IOException//  ww  w. ja v a2  s.  c o  m
 */
@Test
public void testAssembleZipBag() throws IOException, CompressorException, ArchiveException {
    PackageGenerationParameters params = new PackageGenerationParameters();
    setupCommonPackageParams(params);
    params.addParam(GeneralParameterNames.ARCHIVING_FORMAT, ArchiveStreamFactory.ZIP);
    params.addParam(GeneralParameterNames.CHECKSUM_ALGORITHMS, checksumAlg);
    params.addParam(GeneralParameterNames.CHECKSUM_ALGORITHMS, "sha1");

    underTest = new BagItPackageAssembler();
    underTest.init(params);

    //Reserve a URI for data file
    String filePath = "myProject/dataFile.txt";
    URI result = underTest.reserveResource(filePath, PackageResourceType.DATA);
    String expectedURI = "file:///" + packageName + "/" + "data" + "/" + filePath;
    log.debug("URI expected: " + expectedURI);
    log.debug("URI result: " + result.toString());
    assertTrue(expectedURI.equals(result.toString()));

    //Put content into the space specified by the URI
    String fileContent = "This is the data file. data data data data data data data data data data data data.";
    underTest.putResource(result, new ByteArrayInputStream(fileContent.getBytes()));

    //Reserve a URI for metadata file
    filePath = "metadataFile.txt";
    result = underTest.reserveResource(filePath, PackageResourceType.METADATA);
    expectedURI = "file:///" + packageName + "/" + filePath;
    assertTrue(expectedURI.equals(result.toString()));

    //Put content into the space specified by the URI
    fileContent = "This is the metadata file. metadata metadata metadata metadata metadata metadata metadata .";
    underTest.putResource(result, new ByteArrayInputStream(fileContent.getBytes()));

    Package pkg = underTest.assemblePackage();

    ArchiveInputStream ais = new ArchiveStreamFactory().createArchiveInputStream(ArchiveStreamFactory.ZIP,
            pkg.serialize());

    Set<String> files = new HashSet<String>();
    ArchiveEntry entry = ais.getNextEntry();
    while (entry != null) {
        files.add(entry.getName().replace("\\", "/"));
        entry = ais.getNextEntry();
    }

    //Make sure that the packageName is set properly (packageName + contentype extension)
    String expectedPackageName = packageName.concat(".zip");
    Assert.assertEquals(expectedPackageName, pkg.getPackageName());

    // There should be 10 files: the 2 files above, bagit.txt, bag-info.txt
    // directories data and data/myProject, 2 manifest files and 2 tag-manifest files
    assertEquals(10, files.size());

    // make sure that expected files are in there
    String pathSep = "/";
    String bagFilePath = packageName + pathSep;
    assertTrue(files.contains(bagFilePath + "bagit.txt"));
    assertTrue(files.contains(bagFilePath + "bag-info.txt"));
    assertTrue(files.contains(bagFilePath + "metadataFile.txt"));
    assertTrue(files.contains(bagFilePath + "data" + pathSep));
    assertTrue(files.contains(bagFilePath + "data" + pathSep + "myProject" + pathSep));
    assertTrue(files.contains(bagFilePath + "data" + pathSep + "myProject" + pathSep + "dataFile.txt"));
}

From source file:org.deventropy.shared.utils.DirectoryArchiverUtil.java

/**
 * Create a zip archive with all the contents of the directory. Optionally push the contents down a directory level
 * or two./*  w  w w.  j av a  2  s  .  c  o m*/
 * 
 * @param archiveFile The final archive file location. The file location must be writable.
 * @param srcDirectory The source directory.
 * @param rootPathPrefix The root prefix. Multiple directory parts should be separated by <code>/</code>.
 * @throws IOException Exception reading the source directory or writing to the destination file.
 */
public static void createZipArchiveOfDirectory(final String archiveFile, final File srcDirectory,
        final String rootPathPrefix) throws IOException {

    createArchiveOfDirectory(archiveFile, srcDirectory, rootPathPrefix, ArchiveStreamFactory.ZIP, UTF_8_NAME,
            null);
}

From source file:org.eclipse.cdt.arduino.core.internal.board.ArduinoManager.java

public static void downloadAndInstall(String url, String archiveFileName, Path installPath,
        IProgressMonitor monitor) throws IOException {
    Exception error = null;/*from ww  w.  jav a2s.c  om*/
    for (int retries = 3; retries > 0 && !monitor.isCanceled(); --retries) {
        try {
            URL dl = new URL(url);
            Path dlDir = ArduinoPreferences.getArduinoHome().resolve("downloads"); //$NON-NLS-1$
            Files.createDirectories(dlDir);
            Path archivePath = dlDir.resolve(archiveFileName);
            URLConnection conn = dl.openConnection();
            conn.setConnectTimeout(10000);
            conn.setReadTimeout(10000);
            Files.copy(conn.getInputStream(), archivePath, StandardCopyOption.REPLACE_EXISTING);

            boolean isWin = Platform.getOS().equals(Platform.OS_WIN32);

            // extract
            ArchiveInputStream archiveIn = null;
            try {
                String compressor = null;
                String archiver = null;
                if (archiveFileName.endsWith("tar.bz2")) { //$NON-NLS-1$
                    compressor = CompressorStreamFactory.BZIP2;
                    archiver = ArchiveStreamFactory.TAR;
                } else if (archiveFileName.endsWith(".tar.gz") || archiveFileName.endsWith(".tgz")) { //$NON-NLS-1$ //$NON-NLS-2$
                    compressor = CompressorStreamFactory.GZIP;
                    archiver = ArchiveStreamFactory.TAR;
                } else if (archiveFileName.endsWith(".tar.xz")) { //$NON-NLS-1$
                    compressor = CompressorStreamFactory.XZ;
                    archiver = ArchiveStreamFactory.TAR;
                } else if (archiveFileName.endsWith(".zip")) { //$NON-NLS-1$
                    archiver = ArchiveStreamFactory.ZIP;
                }

                InputStream in = new BufferedInputStream(new FileInputStream(archivePath.toFile()));
                if (compressor != null) {
                    in = new CompressorStreamFactory().createCompressorInputStream(compressor, in);
                }
                archiveIn = new ArchiveStreamFactory().createArchiveInputStream(archiver, in);

                for (ArchiveEntry entry = archiveIn.getNextEntry(); entry != null; entry = archiveIn
                        .getNextEntry()) {
                    if (entry.isDirectory()) {
                        continue;
                    }

                    // Magic file for git tarballs
                    Path path = Paths.get(entry.getName());
                    if (path.endsWith("pax_global_header")) { //$NON-NLS-1$
                        continue;
                    }

                    // Strip the first directory of the path
                    Path entryPath;
                    switch (path.getName(0).toString()) {
                    case "i586":
                    case "i686":
                        // Cheat for Intel
                        entryPath = installPath.resolve(path);
                        break;
                    default:
                        entryPath = installPath.resolve(path.subpath(1, path.getNameCount()));
                    }

                    Files.createDirectories(entryPath.getParent());

                    if (entry instanceof TarArchiveEntry) {
                        TarArchiveEntry tarEntry = (TarArchiveEntry) entry;
                        if (tarEntry.isLink()) {
                            Path linkPath = Paths.get(tarEntry.getLinkName());
                            linkPath = installPath.resolve(linkPath.subpath(1, linkPath.getNameCount()));
                            Files.deleteIfExists(entryPath);
                            Files.createSymbolicLink(entryPath, entryPath.getParent().relativize(linkPath));
                        } else if (tarEntry.isSymbolicLink()) {
                            Path linkPath = Paths.get(tarEntry.getLinkName());
                            Files.deleteIfExists(entryPath);
                            Files.createSymbolicLink(entryPath, linkPath);
                        } else {
                            Files.copy(archiveIn, entryPath, StandardCopyOption.REPLACE_EXISTING);
                        }
                        if (!isWin && !tarEntry.isSymbolicLink()) {
                            int mode = tarEntry.getMode();
                            Files.setPosixFilePermissions(entryPath, toPerms(mode));
                        }
                    } else {
                        Files.copy(archiveIn, entryPath, StandardCopyOption.REPLACE_EXISTING);
                    }
                }
            } finally {
                if (archiveIn != null) {
                    archiveIn.close();
                }
            }
            return;
        } catch (IOException | CompressorException | ArchiveException e) {
            error = e;
            // retry
        }
    }

    // out of retries
    if (error instanceof IOException) {
        throw (IOException) error;
    } else {
        throw new IOException(error);
    }
}

From source file:org.eclipse.cdt.arduino.core.internal.board.Platform.java

public IStatus install(IProgressMonitor monitor) throws CoreException {
    try {/*from   ww w .  j  a v a  2s  .  c om*/
        try (CloseableHttpClient client = HttpClients.createDefault()) {
            HttpGet get = new HttpGet(url);
            try (CloseableHttpResponse response = client.execute(get)) {
                if (response.getStatusLine().getStatusCode() >= 400) {
                    return new Status(IStatus.ERROR, Activator.getId(),
                            response.getStatusLine().getReasonPhrase());
                } else {
                    HttpEntity entity = response.getEntity();
                    if (entity == null) {
                        return new Status(IStatus.ERROR, Activator.getId(), Messages.ArduinoBoardManager_1);
                    }
                    // the archive has the version number as the root
                    // directory
                    Path installPath = getInstallPath().getParent();
                    Files.createDirectories(installPath);
                    Path archivePath = installPath.resolve(archiveFileName);
                    Files.copy(entity.getContent(), archivePath, StandardCopyOption.REPLACE_EXISTING);

                    // extract
                    ArchiveInputStream archiveIn = null;
                    try {
                        String compressor = null;
                        String archiver = null;
                        if (archiveFileName.endsWith("tar.bz2")) { //$NON-NLS-1$
                            compressor = CompressorStreamFactory.BZIP2;
                            archiver = ArchiveStreamFactory.TAR;
                        } else if (archiveFileName.endsWith(".tar.gz") || archiveFileName.endsWith(".tgz")) { //$NON-NLS-1$ //$NON-NLS-2$
                            compressor = CompressorStreamFactory.GZIP;
                            archiver = ArchiveStreamFactory.TAR;
                        } else if (archiveFileName.endsWith(".tar.xz")) { //$NON-NLS-1$
                            compressor = CompressorStreamFactory.XZ;
                            archiver = ArchiveStreamFactory.TAR;
                        } else if (archiveFileName.endsWith(".zip")) { //$NON-NLS-1$
                            archiver = ArchiveStreamFactory.ZIP;
                        }

                        InputStream in = new BufferedInputStream(new FileInputStream(archivePath.toFile()));
                        if (compressor != null) {
                            in = new CompressorStreamFactory().createCompressorInputStream(compressor, in);
                        }
                        archiveIn = new ArchiveStreamFactory().createArchiveInputStream(archiver, in);

                        for (ArchiveEntry entry = archiveIn.getNextEntry(); entry != null; entry = archiveIn
                                .getNextEntry()) {
                            if (entry.isDirectory()) {
                                continue;
                            }

                            // TODO check for soft links in tar files.
                            Path entryPath = installPath.resolve(entry.getName());
                            Files.createDirectories(entryPath.getParent());
                            Files.copy(archiveIn, entryPath, StandardCopyOption.REPLACE_EXISTING);
                        }
                    } finally {
                        if (archiveIn != null) {
                            archiveIn.close();
                        }
                    }
                }
            }
        }
        return Status.OK_STATUS;
    } catch (IOException | CompressorException | ArchiveException e) {
        throw new CoreException(new Status(IStatus.ERROR, Activator.getId(), "Installing Platform", e));
    }
}

From source file:org.envirocar.app.util.Util.java

/**
 * Android devices up to version 2.3.7 have a bug in the native
 * Zip archive creation, making the archive unreadable with some
 * programs.//from   w ww  .  j a v  a  2  s.co m
 * 
 * @param files
 *            the list of files of the target archive
 * @param target
 *            the target filename
 * @throws IOException
 */
public static void zipInteroperable(List<File> files, String target) throws IOException {
    ZipArchiveOutputStream aos = null;
    OutputStream out = null;
    try {
        File tarFile = new File(target);
        out = new FileOutputStream(tarFile);

        try {
            aos = (ZipArchiveOutputStream) new ArchiveStreamFactory()
                    .createArchiveOutputStream(ArchiveStreamFactory.ZIP, out);
        } catch (ArchiveException e) {
            throw new IOException(e);
        }

        for (File file : files) {
            ZipArchiveEntry entry = new ZipArchiveEntry(file, file.getName());
            entry.setSize(file.length());
            aos.putArchiveEntry(entry);
            IOUtils.copy(new FileInputStream(file), aos);
            aos.closeArchiveEntry();
        }

    } catch (IOException e) {
        throw e;
    } finally {
        aos.finish();
        out.close();
    }
}

From source file:org.interreg.docexplore.util.ZipUtils.java

public static void zip(File directory, File[] files, File zipfile, float[] progress, float progressOffset,
        float progressAmount, int level) throws Exception {
    URI base = directory.toURI();
    Deque<File> queue = new LinkedList<File>();
    OutputStream out = new FileOutputStream(zipfile, false);
    Closeable res = null;//from   ww  w.j  av a  2s  .co m
    try {
        int nEntries = count(files, queue, 0);
        while (!queue.isEmpty()) {
            File dir = queue.pop();
            nEntries = count(dir.listFiles(), queue, nEntries);
        }

        ZipArchiveOutputStream zout = (ZipArchiveOutputStream) new ArchiveStreamFactory()
                .createArchiveOutputStream(ArchiveStreamFactory.ZIP, out);
        zout.setLevel(level);
        res = zout;

        int cnt = zip(files, queue, base, 0, nEntries, progress, progressOffset, progressAmount, zout);
        while (!queue.isEmpty()) {
            File dir = queue.pop();
            cnt = zip(dir.listFiles(), queue, base, cnt, nEntries, progress, progressOffset, progressAmount,
                    zout);
        }
    } finally {
        res.close();
    }
}

From source file:org.jason.mapmaker.server.util.ZipUtil.java

/**
 * Decompress a ZIP file into the temp directory, then return alist of File objects for further
 * processing. User is responsible for cleaning up the files later
 *
 * @param zipFile File object representing the zipped file to be decompressed
 * @return a List<File> containing File objects for each file that was in the zip archive
 * @throws FileNotFoundException zip archive file was not found
 * @throws ArchiveException      something went wrong creating
 *                               the archive input stream
 * @throws IOException/*from www .jav a 2  s.  c om*/
 */
public static List<File> decompress(File zipFile) throws FileNotFoundException, ArchiveException, IOException {

    List<File> archiveContents = new ArrayList<File>();

    // create the input stream for the file, then the input stream for the actual zip file
    final InputStream is = new FileInputStream(zipFile);
    ArchiveInputStream ais = new ArchiveStreamFactory().createArchiveInputStream(ArchiveStreamFactory.ZIP, is);

    // cycle through the entries in the zip archive and write them to the system temp dir
    ZipArchiveEntry entry = (ZipArchiveEntry) ais.getNextEntry();
    while (entry != null) {
        File outputFile = new File(tmpFile, entry.getName()); // don't do this anonymously, need it for the list
        OutputStream os = new FileOutputStream(outputFile);

        IOUtils.copy(ais, os); // copy from the archiveinputstream to the output stream
        os.close(); // close the output stream

        archiveContents.add(outputFile);

        entry = (ZipArchiveEntry) ais.getNextEntry();
    }

    ais.close();
    is.close();

    return archiveContents;
}

From source file:org.jason.mapmaker.server.util.ZipUtil.java

/**
 * Decompress a zip file from a remote URL.
 *
 * @param url       remote URL to import the file from
 * @return          a List<File> containing File objects for each file that was in the zip archive
 * @throws FileNotFoundException    zip archive file was not found
 * @throws ArchiveException         something went wrong creating
 *                                  the archive input stream
 * @throws IOException//from w  w  w  .j  a  v  a 2 s .  co  m
 */
public static List<File> decompress(URL url) throws FileNotFoundException, ArchiveException, IOException {

    List<File> archiveContents = new ArrayList<File>();
    final InputStream is = new BufferedInputStream(url.openStream(), 1024);
    ArchiveInputStream ais = new ArchiveStreamFactory().createArchiveInputStream(ArchiveStreamFactory.ZIP, is);

    // cycle through the entries in the zip archive and write them to the system temp dir
    ZipArchiveEntry entry = (ZipArchiveEntry) ais.getNextEntry();
    while (entry != null) {
        File outputFile = new File(tmpFile, entry.getName()); // don't do this anonymously, need it for the list
        OutputStream os = new FileOutputStream(outputFile);

        IOUtils.copy(ais, os); // copy from the archiveinputstream to the output stream
        os.close(); // close the output stream

        archiveContents.add(outputFile);

        entry = (ZipArchiveEntry) ais.getNextEntry();
    }

    ais.close();
    is.close();

    return archiveContents;
}

From source file:org.jason.mapmaker.server.util.ZipUtil.java

/**
 * Compress a List of File objects into a single ZIP file. User is responsible for deleting the files which
 * have been compressed/*  www .  java 2  s  .  com*/
 *
 * @param filename filename for the generated zip file
 * @param fileList List<File> containing the File objects to add to the archive
 * @return a File object representing the zip archive file
 * @throws FileNotFoundException unable to find the file when creating the OutputStream
 * @throws ArchiveException      something went wrong creating
 *                               the output archive
 * @throws IOException
 */
public static File compress(String filename, List<File> fileList) throws FileNotFoundException,

        ArchiveException,

        IOException {

    // Create the File object that will later be returned
    File zipfile = new File(tmpFile, filename);

    // create the output stream and the archiveoutput stream (as a zip file)
    final OutputStream os = new FileOutputStream(zipfile);
    ArchiveOutputStream aos = new ArchiveStreamFactory().createArchiveOutputStream(ArchiveStreamFactory.ZIP,
            os);

    // cycle throught the files in the list
    for (File file : fileList) {
        // add an anonymous ZipArchiveEntry to the archiveoutputstream, then copy the streams
        aos.putArchiveEntry(new ZipArchiveEntry(file.getName()));
        IOUtils.copy(new FileInputStream(file), aos);
        aos.closeArchiveEntry();

    }

    // cleanup. Everybody do their part.
    aos.close();
    os.close();

    return zipfile;
}

From source file:org.jwebsocket.util.Tools.java

/**
 * Compress a byte array using zip compression
 *
 * @param aBA//from w w w .j  a v a  2  s. c om
 * @param aBase64Encode if TRUE, the result is Base64 encoded
 * @return
 * @throws Exception
 */
public static byte[] zip(byte[] aBA, Boolean aBase64Encode) throws Exception {
    ByteArrayOutputStream lBAOS = new ByteArrayOutputStream();
    ArchiveOutputStream lAOS = new ArchiveStreamFactory().createArchiveOutputStream(ArchiveStreamFactory.ZIP,
            lBAOS);
    ZipArchiveEntry lZipEntry = new ZipArchiveEntry("temp.zip");
    lZipEntry.setSize(aBA.length);
    lAOS.putArchiveEntry(lZipEntry);
    lAOS.write(aBA);
    lAOS.closeArchiveEntry();
    lAOS.flush();
    lAOS.close();

    if (aBase64Encode) {
        aBA = Base64.encodeBase64(lBAOS.toByteArray());
    } else {
        aBA = lBAOS.toByteArray();
    }

    return aBA;
}