Example usage for org.apache.commons.compress.archivers ArchiveOutputStream close

List of usage examples for org.apache.commons.compress.archivers ArchiveOutputStream close

Introduction

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

Prototype

public void close() throws IOException 

Source Link

Document

Closes this output stream and releases any system resources associated with this stream.

Usage

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

private void writeZip2Client(VWBContext context, String tname, int[] rids, HttpServletResponse resp) {
    Team team = teamService.getTeamByName(tname);
    setResponseHeader(tname + ".zip", resp);
    ArchiveOutputStream out = getArchiveOutputStream(resp);
    writeCss(tname, out);/*from ww  w.j  a  v  a  2  s . com*/
    Map<String, String> id2Title = new HashMap<String, String>();
    // ?
    writeForResource(tname, team.getId(), rids, context, out, id2Title, null, false);
    // 
    writeCatalog(tname, id2Title, out);
    writeOverview(DEFAULT_TAG, tname, rids, out, context, false);
    try {
        out.close();
    } catch (IOException e) {
        LOG.error("", e);
    }
}

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

private void writeEpub2Client(VWBContext context, String tname, Map<String, List<Tag>> tagMap,
        HttpServletResponse resp) {/*w w w.  j a v a 2  s .  c o  m*/
    Team team = teamService.getTeamByName(tname);
    setResponseHeader(tname + ".epub", resp);
    ArchiveOutputStream out = getArchiveOutputStream(resp);
    writeCss(tname, out);
    Map<String, String> id2Title = new HashMap<String, String>();
    List<String> allPages = new ArrayList<String>();
    writeForTag(tname, tagMap, team.getId(), context, out, id2Title, allPages, true);
    writeOverview(tname, tagMap, out, context, true);
    writeEpubFiles(tname, allPages, out, id2Title);
    try {
        out.close();
    } catch (IOException e) {
        LOG.error(e.getMessage(), e);
    }
}

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

private void writeEpub2Client(VWBContext context, String tname, int[] rids, HttpServletResponse resp) {
    Team team = teamService.getTeamByName(tname);
    setResponseHeader(tname + ".epub", resp);
    ArchiveOutputStream out = getArchiveOutputStream(resp);
    writeCss(tname, out);/*  ww  w. ja va2  s  . c  o  m*/
    Map<String, String> id2Title = new HashMap<String, String>();
    List<String> allPages = new ArrayList<String>();
    writeForResource(tname, team.getId(), rids, context, out, id2Title, allPages, true);
    writeOverview(DEFAULT_TAG, tname, rids, out, context, true);
    writeEpubFiles(tname, allPages, out, id2Title);
    try {
        out.close();
    } catch (IOException e) {
        LOG.error(e.getMessage(), e);
    }
}

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

/**
 * Override to make use of the ZipArchive#createArchiveOutputStream(Path path) method
 * instead of the method ZipArchive#createArchiveOutputStream(BufferedOutputStream bufferedOutputStream).
 *///from w ww  . java2s  .c om
@Override
public Path compress(Path... paths) throws IOException {
    Path compress = null;
    ArchiveOutputStream archiveOutputStream = null;

    for (Path path : paths) {
        // get path infos
        final Path parent = path.getParent();
        final String name = path.getFileName().toString();
        final boolean isDirectory = isDirectory(path);

        if (compress == null) {
            // create compress file
            String compressName = (paths.length == 1 ? name : getName());
            compress = Paths.get(parent.toString(), compressName + getExtension());

            // creates a new compress file to not override if already exists
            // if you do not want this behavior, just comment this line
            compress = createFile(ArchiveAction.COMPRESS, parent, compress);

            // open compress file stream
            archiveOutputStream = createArchiveOutputStream(compress);

            logger.debug("creating the archive file " + compressName);
        }

        logger.debug("reading path " + path);

        if (isDirectory) {
            compressDirectory(parent, path, archiveOutputStream);
        } else {
            compressFile(parent, path, archiveOutputStream);
        }
    }

    // closing streams
    if (archiveOutputStream != null) {
        archiveOutputStream.finish();
        archiveOutputStream.close();
    }

    logger.debug("finishing the archive file " + compress);

    return compress;
}

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

/**
 * Generic compress implementation/*  w ww .j  av  a2  s .  c o m*/
 */
@Override
public Path compress(Path... paths) throws IOException {
    Path compress = null;
    ArchiveOutputStream archiveOutputStream = null;

    for (Path path : paths) {
        // get path infos
        final Path parent = path.getParent();
        final String name = path.getFileName().toString();
        final boolean isDirectory = isDirectory(path);

        if (compress == null) {
            // create compress file
            String compressName = (paths.length == 1 ? name : getName());
            compress = Paths.get(parent.toString(), compressName + getExtension());

            // creates a new compress file to not override if already exists
            // if you do not want this behavior, just comment this line
            compress = createFile(ArchiveAction.COMPRESS, parent, compress);

            // open compress file stream
            archiveOutputStream = createArchiveOutputStream(
                    new BufferedOutputStream(newOutputStream(compress)));

            logger.debug("creating the archive file " + compressName);
        }

        logger.debug("reading path " + path);

        if (isDirectory) {
            compressDirectory(parent, path, archiveOutputStream);
        } else {
            compressFile(parent, path, archiveOutputStream);
        }
    }

    // closing streams
    if (archiveOutputStream != null) {
        archiveOutputStream.finish();
        archiveOutputStream.close();
    }

    logger.debug("finishing the archive file: " + compress);

    return compress;
}

From source file:io.github.jeremgamer.maintenance.Maintenance.java

public void backupProcess(CommandSender sender) {

    Calendar cal = Calendar.getInstance();
    DateFormat dateFormat = new SimpleDateFormat("yyyy MM dd - HH mm ss");
    String zipFile = new File("").getAbsolutePath() + "/backups/" + dateFormat.format(cal.getTime()) + ".zip";
    File zip = new File(zipFile);
    String srcDir = new File("").getAbsolutePath();

    try {//from  w  ww .ja  v a  2 s  . c  o  m

        try {
            zip.createNewFile();
        } catch (IOException e) {
            e.printStackTrace();
        }

        File dir = new File(srcDir);

        List<File> filesList = (List<File>) FileUtils.listFilesAndDirs(dir, TrueFileFilter.INSTANCE,
                TrueFileFilter.INSTANCE);
        File[] files = filesList.toArray(new File[filesList.size()]);

        OutputStream out = new FileOutputStream(zip);
        ArchiveOutputStream zipOutput = new ZipArchiveOutputStream(out);
        String filePath;

        for (int i = 0; i < files.length; i++) {
            if (files[i].isDirectory()) {

            } else if (files[i].getAbsolutePath().contains(new File("").getAbsolutePath() + "\\backups\\")) {

            } else {

                filePath = files[i].getAbsolutePath().substring(srcDir.length() + 1);

                try {
                    ZipArchiveEntry entry = new ZipArchiveEntry(filePath);
                    entry.setSize(new File(files[i].getAbsolutePath()).length());
                    zipOutput.putArchiveEntry(entry);
                    IOUtils.copy(new FileInputStream(files[i].getAbsolutePath()), zipOutput);
                    zipOutput.closeArchiveEntry();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        zipOutput.finish();
        zipOutput.close();
        out.close();
    } catch (IOException ioe) {
        ioe.printStackTrace();
    } catch (IllegalArgumentException iae) {
        iae.printStackTrace();
    }

    sender.sendMessage(getConfig().getString("backupSuccess").replaceAll("&", ""));
    getLogger().info("Backup success!");
}

From source file:big.BigZip.java

/**
  * Copies one file into the big archive
  * @param fileToCopy/*from ww w. j  a  va2 s . c  o  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:big.BigZip.java

/**
 * Copies one file into the big archive//from  w  ww . j ava2  s.c  o  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;
}

From source file:big.BigZip.java

/**
 * Copies one file into the big archive/*from  w w w .  j  ava  2 s . c  o m*/
 * @param stream
 * @param SHA1
 * @param filePathToWriteInTextLine
 * @return 
 * @throws java.io.IOException 
 */
public boolean quickWriteGenericStream(final InputStream stream, final String SHA1,
        final String filePathToWriteInTextLine) throws IOException {
    // declare
    ByteArrayOutputStream outputZipStream = new ByteArrayOutputStream();
    ByteArrayInputStream byteInput = null;
    try {
        // save this operation on the log of commits
        addTagStarted(filePathToWriteInTextLine);
        // Create Archive Output Stream that attaches File Output Stream / and specifies type of compression
        ArchiveOutputStream logical_zip = new ArchiveStreamFactory()
                .createArchiveOutputStream(ArchiveStreamFactory.ZIP, outputZipStream);
        // Create Archive entry - write header information
        ZipArchiveEntry zipArchiveEntry = new ZipArchiveEntry(filePathToWriteInTextLine);
        logical_zip.putArchiveEntry(zipArchiveEntry);
        // Copy input file

        IOUtils.copy(stream, logical_zip);

        logical_zip.closeArchiveEntry();
        logical_zip.finish();
        logical_zip.flush();
        logical_zip.close();

        // get the bytes
        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;
        }

        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: " + filePathToWriteInTextLine);
        return false;
    } finally {
        if (byteInput != null) {
            byteInput.close();
        }
        stream.close();
        outputZipStream.close();
        outputStream.close();
    }
    return true;
}

From source file:edu.ur.ir.ir_export.service.DefaultCollectionExportService.java

/**
 * Export all collections in the repository.
 * /*from w  ww . j a v  a 2 s .com*/
 * @param repository - repository to export
 * @throws IOException 
 */
public void export(Repository repository, File zipFileDestination) throws IOException {
    // create the path if it doesn't exist
    String path = FilenameUtils.getPath(zipFileDestination.getCanonicalPath());
    if (!path.equals("")) {
        File pathOnly = new File(FilenameUtils.getFullPath(zipFileDestination.getCanonicalPath()));
        FileUtils.forceMkdir(pathOnly);
    }

    File collectionXmlFile = temporaryFileCreator.createTemporaryFile(extension);
    Set<FileInfo> allPictures = createXmlFile(collectionXmlFile, repository.getInstitutionalCollections(),
            true);

    FileOutputStream out = new FileOutputStream(zipFileDestination);
    ArchiveOutputStream os = null;
    try {
        os = new ArchiveStreamFactory().createArchiveOutputStream("zip", out);
        os.putArchiveEntry(new ZipArchiveEntry("collection.xml"));

        FileInputStream fis = null;
        try {
            log.debug("adding xml file");
            fis = new FileInputStream(collectionXmlFile);
            IOUtils.copy(fis, os);
        } finally {
            if (fis != null) {
                fis.close();
                fis = null;
            }
        }

        log.debug("adding pictures size " + allPictures.size());
        for (FileInfo fileInfo : allPictures) {
            File f = new File(fileInfo.getFullPath());
            String name = FilenameUtils.getName(fileInfo.getFullPath());
            name = name + '.' + fileInfo.getExtension();
            log.debug(" adding name " + name);
            os.putArchiveEntry(new ZipArchiveEntry(name));
            try {
                log.debug("adding input stream");
                fis = new FileInputStream(f);
                IOUtils.copy(fis, os);
            } finally {
                if (fis != null) {
                    fis.close();
                    fis = null;
                }
            }
        }

        os.closeArchiveEntry();
        out.flush();
    } catch (ArchiveException e) {
        throw new IOException(e);
    } finally {
        if (os != null) {
            os.close();
            os = null;
        }
    }

    FileUtils.deleteQuietly(collectionXmlFile);

}