Example usage for org.apache.commons.compress.archivers.sevenz SevenZOutputFile createArchiveEntry

List of usage examples for org.apache.commons.compress.archivers.sevenz SevenZOutputFile createArchiveEntry

Introduction

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

Prototype

public SevenZArchiveEntry createArchiveEntry(final File inputFile, final String entryName) throws IOException 

Source Link

Document

Create an archive entry using the inputFile and entryName provided.

Usage

From source file:com.espringtran.compressor4j.processor.SevenZipProcessor.java

/**
 * Compress data// w  w  w.  ja v a  2 s . c o  m
 * 
 * @param fileCompressor
 *            FileCompressor object
 * @return
 * @throws Exception
 */
@Override
public byte[] compressData(FileCompressor fileCompressor) throws Exception {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    SevenZOutputFile sevenZOutput = new SevenZOutputFile(new File(fileCompressor.getCompressedPath()));
    try {
        for (BinaryFile binaryFile : fileCompressor.getMapBinaryFile().values()) {
            SevenZArchiveEntry entry = sevenZOutput.createArchiveEntry(new File(binaryFile.getSrcPath()),
                    binaryFile.getDesPath());
            entry.setSize(binaryFile.getActualSize());
            sevenZOutput.putArchiveEntry(entry);
            sevenZOutput.write(binaryFile.getData());
            sevenZOutput.closeArchiveEntry();
        }
        sevenZOutput.finish();
    } catch (Exception e) {
        FileCompressor.LOGGER.error("Error on compress data", e);
    } finally {
        sevenZOutput.close();
        baos.close();
    }
    return baos.toByteArray();
}

From source file:inventory.pl.services.BackupService.java

private void archive(File outputFile) {
    try {//from ww  w .jav a 2 s. c o m
        byte[] buffer = new byte[1024];

        File f;
        SevenZOutputFile sevenZOutput = new SevenZOutputFile(outputFile);

        System.out.println("Output to Zip : " + outputFile);

        long startTime = System.currentTimeMillis();
        for (String file : this.entries) {
            f = new File(SOURCE_FOLDER + File.separator + file);
            System.out.println("File Added : " + f.getAbsolutePath());
            SevenZArchiveEntry ze = sevenZOutput.createArchiveEntry(f, file);

            try {
                FileInputStream in = new FileInputStream(SOURCE_FOLDER + File.separator + file);
                sevenZOutput.putArchiveEntry(ze);
                int len;
                while ((len = in.read(buffer)) > 0) {
                    sevenZOutput.write(buffer, 0, len);
                }
                sevenZOutput.closeArchiveEntry();
                in.close();

            } catch (java.util.zip.ZipException zipException) {
                System.err.println("ex=" + zipException.getMessage());
            }

        }

        long endTime = System.currentTimeMillis();

        sevenZOutput.close();
        System.out.println("Done in " + (endTime - startTime));
    } catch (IOException ex) {
        Logger.getLogger(BackupService.class.getName()).log(Level.SEVERE, null, ex);
    }

}