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

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

Introduction

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

Prototype

ArchiveStreamFactory

Source Link

Usage

From source file:com.geewhiz.pacify.utils.ArchiveUtils.java

private static Boolean archiveContainsFile(File archive, String archiveType, String fileToLookFor) {
    ArchiveInputStream ais = null;//  ww w . ja  v  a2s.  c  om
    try {
        ArchiveStreamFactory factory = new ArchiveStreamFactory();

        ais = factory.createArchiveInputStream(archiveType, new FileInputStream(archive));

        ArchiveEntry entry;
        while ((entry = ais.getNextEntry()) != null) {
            if (fileToLookFor.equals(entry.getName())) {
                return Boolean.TRUE;
            }
        }

        return Boolean.FALSE;
    } catch (ArchiveException e) {
        throw new RuntimeException(e);
    } catch (FileNotFoundException e) {
        throw new RuntimeException(e);
    } catch (IOException e) {
        throw new RuntimeException(e);
    } finally {
        IOUtils.closeQuietly(ais);
    }
}

From source file:cpcc.vvrte.services.VirtualVehicleMigratorImpl.java

/**
 * {@inheritDoc}//from  ww w . ja va 2s . c  om
 */
@Override
public void storeChunk(InputStream inStream) throws ArchiveException, IOException {
    boolean lastChunk = false;
    String chunkName = "unknown-" + System.currentTimeMillis();
    ArchiveStreamFactory f = new ArchiveStreamFactory();
    ArchiveInputStream ais = f.createArchiveInputStream("tar", inStream);
    VirtualVehicleHolder virtualVehicleHolder = new VirtualVehicleHolder();

    for (TarArchiveEntry entry = (TarArchiveEntry) ais
            .getNextEntry(); entry != null; entry = (TarArchiveEntry) ais.getNextEntry()) {
        chunkName = entry.getName();

        if (chunkName.startsWith("vv/")) {
            lastChunk |= storeVirtualVehicleEntry(ais, entry, virtualVehicleHolder);
            logMigratedChunk(chunkName, virtualVehicleHolder.getVirtualVehicle(), lastChunk);
        } else if (chunkName.startsWith("storage/")) {
            storeStorageEntry(ais, entry, virtualVehicleHolder.getVirtualVehicle());
            logMigratedChunk(chunkName, virtualVehicleHolder.getVirtualVehicle(), lastChunk);
        }
        // TODO message queue
        else {
            throw new IOException("Can not store unknown type of entry " + chunkName);
        }
    }

    sessionManager.commit();

    VirtualVehicle vv = virtualVehicleHolder.getVirtualVehicle();

    String result = new JSONObject("uuid", vv.getUuid(), "chunk", vv.getChunkNumber()).toCompactString();

    CommunicationResponse response = com.transfer(vv.getMigrationSource(),
            VvRteConstants.MIGRATION_ACK_CONNECTOR,
            org.apache.commons.codec.binary.StringUtils.getBytesUtf8(result));

    if (response.getStatus() == Status.OK) {
        if (lastChunk) {
            VirtualVehicleState newState = VirtualVehicleState.VV_NO_CHANGE_AFTER_MIGRATION
                    .contains(vv.getPreMigrationState()) ? vv.getPreMigrationState()
                            : VirtualVehicleState.MIGRATION_COMPLETED_RCV;

            vv.setPreMigrationState(null);
            updateStateAndCommit(vv, newState, null);
            launcher.stateChange(vv.getId(), vv.getState());
        }
    } else {
        String content = new String(response.getContent(), "UTF-8");
        logger.error("Migration ACK failed! Virtual vehicle: " + vv.getName() + " (" + vv.getUuid() + ") "
                + content);
        updateStateAndCommit(vv, VirtualVehicleState.MIGRATION_INTERRUPTED_RCV, content);
    }

}

From source file:gdt.data.entity.ArchiveHandler.java

/**
 * Compress the database into the tar archive file. 
 * @param  entigrator entigrator instance,
 * @param locator$ container of arguments 
 * in the string form. /*from  ww  w . ja  v  a 2s.  co m*/
 * @return true if success false otherwise.
 */
public boolean compressDatabaseToTar(Entigrator entigrator, String locator$) {
    try {
        System.out.println("ArchiveHandler:compressDatabaseToTar:locator=" + locator$);
        Properties locator = Locator.toProperties(locator$);
        archiveType$ = locator.getProperty(ARCHIVE_TYPE);
        archiveFile$ = locator.getProperty(ARCHIVE_FILE);
        String tarfile$ = archiveFile$;
        File tarfile = new File(tarfile$);
        if (!tarfile.exists())
            tarfile.createNewFile();
        TarArchiveOutputStream aos = (TarArchiveOutputStream) new ArchiveStreamFactory()
                .createArchiveOutputStream("tar", new FileOutputStream(tarfile$));
        aos.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU);
        String entihome$ = entigrator.getEntihome();
        append(entigrator, entihome$, entihome$, aos);
        aos.close();
        return true;
    } catch (Exception e) {
        Logger.getLogger(getClass().getName()).severe(e.toString());
        return false;
    }
}

From source file:gdt.data.entity.ArchiveHandler.java

/**
   * Compress the database into the tgz archive file. 
   * @param  entigrator entigrator instance
   * @param locator$ container of arguments in the string form. 
   * @return true if success false otherwise.
   *//*w  w  w.j  a v a  2  s .c o  m*/
public boolean compressDatabaseToTgz(Entigrator entigrator, String locator$) {
    try {
        Properties locator = Locator.toProperties(locator$);
        archiveType$ = locator.getProperty(ARCHIVE_TYPE);
        archiveFile$ = locator.getProperty(ARCHIVE_FILE);
        String tgzFile$ = archiveFile$;
        File tgzFile = new File(tgzFile$);
        if (!tgzFile.exists())
            tgzFile.createNewFile();
        // String userHome$=System.getProperty("user.home");
        File tarFile = new File(tgzFile$.replace(".tgz", "") + ".tar");
        if (!tarFile.exists())
            tarFile.createNewFile();
        TarArchiveOutputStream aos = (TarArchiveOutputStream) new ArchiveStreamFactory()
                .createArchiveOutputStream("tar", new FileOutputStream(tarFile));
        aos.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU);
        String entihome$ = entigrator.getEntihome();
        append(entigrator, entihome$, entihome$, aos);
        aos.close();
        compressGzipFile(tarFile.getPath(), tgzFile.getPath());
        tarFile.delete();
        return true;
    } catch (Exception e) {
        Logger.getLogger(getClass().getName()).severe(e.toString());
        return false;
    }
}

From source file:gdt.data.entity.ArchiveHandler.java

/**
   * Compress the entities into the tar archive file. 
   * @param  entigrator entigrator instance
   * @param locator$ container of arguments in the string form. 
   * @return true if success false otherwise.
   *//*w  ww  . j a  v  a 2  s . c  om*/
public boolean compressEntitiesToTar(Entigrator entigrator, String locator$) {
    try {
        //       System.out.println("ArchiveHandler:compressEntitiesToTar:locator="+locator$);
        Properties locator = Locator.toProperties(locator$);
        archiveType$ = locator.getProperty(ARCHIVE_TYPE);
        archiveFile$ = locator.getProperty(ARCHIVE_FILE);
        String entityList$ = locator.getProperty(EntityHandler.ENTITY_LIST);
        String[] sa = Locator.toArray(entityList$);
        System.out.println("ArchiveHandler:compressEntitiesToTar:sa=" + sa.length);
        String tarfile$ = archiveFile$;
        File tarfile = new File(tarfile$);
        if (!tarfile.exists())
            tarfile.createNewFile();
        String entityBody$ = null;
        String entityHome$ = null;
        TarArchiveOutputStream aos = (TarArchiveOutputStream) new ArchiveStreamFactory()
                .createArchiveOutputStream("tar", new FileOutputStream(tarfile$));
        aos.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU);
        String entihome$ = entigrator.getEntihome();
        String entitiesHome$ = entihome$ + "/" + Entigrator.ENTITY_BASE + "/data/";
        //  System.out.println("ArchiveHandler:append:entities home=" + entitiesHome$);
        String iconsHome$ = entihome$ + "/" + Entigrator.ICONS + "/";
        String icon$;
        for (String aSa : sa) {
            entityBody$ = entitiesHome$ + aSa;
            append(entigrator, entigrator.getEntihome(), entityBody$, aos);
            entityHome$ = entigrator.ent_getHome(aSa);
            if (new File(entityHome$).exists()) {
                append(entigrator, entigrator.getEntihome(), entityHome$, aos);
            }
            icon$ = entigrator.indx_getIcon(aSa);
            if (icon$ != null)
                append(entigrator, entigrator.getEntihome(), iconsHome$ + icon$, aos);
        }
        aos.close();
        return true;
    } catch (Exception e) {
        LOGGER.severe(e.toString());
        return false;
    }
}

From source file:big.BigZip.java

/**
  * Copies one file into the big archive
  * @param fileToCopy/* ww  w.  j a  v  a 2  s  .co m*/
  * @return 
  */
public boolean writeFile(final File fileToCopy) {

    // declare
    ByteArrayOutputStream outputZipStream = new ByteArrayOutputStream();
    try {
        /* Create Archive Output Stream that attaches File Output Stream / and specifies type of compression */
        ArchiveOutputStream logical_zip = new ArchiveStreamFactory()
                .createArchiveOutputStream(ArchiveStreamFactory.ZIP, outputZipStream);
        /* Create Archieve entry - write header information*/
        logical_zip.putArchiveEntry(new ZipArchiveEntry(fileToCopy.getName()));
        /* Copy input file */
        IOUtils.copy(new FileInputStream(fileToCopy), logical_zip);
        logical_zip.closeArchiveEntry();
        logical_zip.finish();
        logical_zip.flush();
        logical_zip.close();

        // get the bytes
        final ByteArrayInputStream byteInput = new ByteArrayInputStream(outputZipStream.toByteArray());

        byte[] buffer = new byte[8192];
        int length, counter = 0;
        // add the magic number to this file block
        outputStream.write(magicSignature.getBytes());
        // now copy the whole file into the BIG archive
        while ((length = byteInput.read(buffer)) > 0) {
            outputStream.write(buffer, 0, length);
            counter += length;
        }
        // if there is something else to be flushed, do it now
        outputStream.flush();

        // calculate the base path
        final String resultingPath = fileToCopy.getAbsolutePath().replace(basePath, "");

        // calculate the SHA1 signature
        final String output = utils.hashing.checksum.generateFileChecksum("SHA-1", fileToCopy);

        // write a new line in our index file
        writerFileIndex.write(
                "\n" + utils.files.getPrettyFileSize(currentPosition) + " " + output + " " + resultingPath);
        // increase the position counter
        currentPosition += counter + magicSignature.length();
    } catch (Exception e) {
        System.err.println("BIG346 - Error copying file: " + fileToCopy.getAbsolutePath());
        return false;
    }

    finally {
    }
    return true;
}

From source file:gdt.data.entity.ArchiveHandler.java

/**
* Compress the entities into the tgz archive file. 
* @param  entigrator entigrator instance 
* @param locator$ container of arguments in the string form. 
* @return true if success false otherwise.
*//*from  www. ja v a  2 s .c  o  m*/
public boolean compressEntitiesToTgz(Entigrator entigrator, String locator$) {
    try {
        Properties locator = Locator.toProperties(locator$);
        archiveType$ = locator.getProperty(ARCHIVE_TYPE);
        archiveFile$ = locator.getProperty(ARCHIVE_FILE);
        String entityList$ = locator.getProperty(EntityHandler.ENTITY_LIST);
        String[] sa = Locator.toArray(entityList$);
        String tgzFile$ = archiveFile$;
        File tgzFile = new File(tgzFile$);
        if (!tgzFile.exists())
            tgzFile.createNewFile();
        // String userHome$=System.getProperty("user.home");
        File tarFile = new File(tgzFile$.replace(".tgz", "") + ".tar");
        if (!tarFile.exists())
            tarFile.createNewFile();
        String entityBody$ = null;
        String entityHome$ = null;
        TarArchiveOutputStream aos = (TarArchiveOutputStream) new ArchiveStreamFactory()
                .createArchiveOutputStream("tar", new FileOutputStream(tarFile));
        aos.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU);
        String entihome$ = entigrator.getEntihome();
        String entitiesHome$ = entihome$ + "/" + Entigrator.ENTITY_BASE + "/data/";
        String iconsHome$ = entihome$ + "/" + Entigrator.ICONS + "/";
        String icon$;
        for (String aSa : sa) {
            entityBody$ = entitiesHome$ + aSa;
            append(entigrator, entigrator.getEntihome(), entityBody$, aos);
            entityHome$ = entigrator.ent_getHome(aSa);
            if (new File(entityHome$).exists())
                append(entigrator, entigrator.getEntihome(), entityHome$, aos);
            icon$ = entigrator.indx_getIcon(aSa);
            if (icon$ != null)
                append(entigrator, entigrator.getEntihome(), iconsHome$ + icon$, aos);

        }
        aos.close();
        compressGzipFile(tarFile.getPath(), tgzFile.getPath());
        tarFile.delete();
        return true;
    } catch (Exception e) {
        LOGGER.severe(e.toString());
        return false;
    }
}

From source file:com.android.tradefed.util.FileUtil.java

public static void extractTarGzip(File tarGzipFile, File destDir)
        throws FileNotFoundException, IOException, ArchiveException {
    GZIPInputStream gzipIn = null;
    ArchiveInputStream archivIn = null;//from   w w  w  .  java  2s. c o  m
    BufferedInputStream buffIn = null;
    BufferedOutputStream buffOut = null;
    try {
        gzipIn = new GZIPInputStream(new BufferedInputStream(new FileInputStream(tarGzipFile)));
        archivIn = new ArchiveStreamFactory().createArchiveInputStream("tar", gzipIn);
        buffIn = new BufferedInputStream(archivIn);
        TarArchiveEntry entry = null;
        while ((entry = (TarArchiveEntry) archivIn.getNextEntry()) != null) {
            String entryName = entry.getName();
            String[] engtryPart = entryName.split("/");
            StringBuilder fullPath = new StringBuilder();
            fullPath.append(destDir.getAbsolutePath());
            for (String e : engtryPart) {
                fullPath.append(File.separator);
                fullPath.append(e);
            }
            File destFile = new File(fullPath.toString());
            if (entryName.endsWith("/")) {
                if (!destFile.exists())
                    destFile.mkdirs();
            } else {
                if (!destFile.exists())
                    destFile.createNewFile();
                buffOut = new BufferedOutputStream(new FileOutputStream(destFile));
                byte[] buf = new byte[8192];
                int len = 0;
                while ((len = buffIn.read(buf)) != -1) {
                    buffOut.write(buf, 0, len);
                }
                buffOut.flush();
            }
        }
    } finally {
        if (buffOut != null) {
            buffOut.close();
        }
        if (buffIn != null) {
            buffIn.close();
        }
        if (archivIn != null) {
            archivIn.close();
        }
        if (gzipIn != null) {
            gzipIn.close();
        }
    }

}

From source file:net.duckling.ddl.service.export.impl.ExportServiceImpl.java

private ArchiveOutputStream getArchiveOutputStream(HttpServletResponse resp) {
    ArchiveOutputStream aos = null;/*from   w  w  w. j  ava2 s .c  om*/
    try {
        aos = new ArchiveStreamFactory().createArchiveOutputStream(ArchiveStreamFactory.ZIP,
                resp.getOutputStream());
    } catch (IOException e) {
        LOG.error(e.getMessage(), e);
    } catch (ArchiveException e) {
        LOG.error(e.getMessage(), e);
    }
    return aos;
}

From source file:big.BigZip.java

/**
 * Copies one file into the big archive/*from  w w  w.  ja v a2  s  .  co m*/
 * @param fileToCopy
 * @param SHA1
 * @param filePathToWriteInTextLine
 * @return 
 */
public boolean quickWrite(final File fileToCopy, final String SHA1, final String filePathToWriteInTextLine) {
    // declare
    ByteArrayOutputStream outputZipStream = new ByteArrayOutputStream();
    try {
        // save this operation on the log of commits
        addTagStarted(fileToCopy.getName());
        //pointRestoreAndSave(fileToCopy);

        /* Create Archive Output Stream that attaches File Output Stream / and specifies type of compression */
        ArchiveOutputStream logical_zip = new ArchiveStreamFactory()
                .createArchiveOutputStream(ArchiveStreamFactory.ZIP, outputZipStream);
        /* Create Archieve entry - write header information*/
        logical_zip.putArchiveEntry(new ZipArchiveEntry(fileToCopy.getName()));
        /* Copy input file */
        IOUtils.copy(new FileInputStream(fileToCopy), logical_zip);
        logical_zip.closeArchiveEntry();
        logical_zip.finish();
        logical_zip.flush();
        logical_zip.close();

        // get the bytes
        final ByteArrayInputStream byteInput = new ByteArrayInputStream(outputZipStream.toByteArray());

        byte[] buffer = new byte[8192];
        int length, counter = 0;
        // add the magic number to this file block
        outputStream.write(magicSignature.getBytes());
        // now copy the whole file into the BIG archive
        while ((length = byteInput.read(buffer)) > 0) {
            outputStream.write(buffer, 0, length);
            counter += length;
        }
        // if there is something else to be flushed, do it now
        //outputStream.flush();

        // calculate the base path
        //final String resultingPath = fileToCopy.getAbsolutePath().replace(rootFolder, "");

        final String line = "\n" + utils.files.getPrettyFileSize(currentPosition) + " " + SHA1 + " "
                + filePathToWriteInTextLine;

        // write a new line in our index file
        writerFileIndex.write(line);
        //writer.flush();
        // increase the position counter
        currentPosition += counter + magicSignature.length();

        // close the log with success
        addTagEnded();
    } catch (Exception e) {
        System.err.println("BIG600 - Error copying file: " + fileToCopy.getAbsolutePath());
        return false;
    } finally {
    }
    return true;
}