Example usage for org.apache.commons.compress.archivers.sevenz SevenZFile read

List of usage examples for org.apache.commons.compress.archivers.sevenz SevenZFile read

Introduction

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

Prototype

public int read(final byte[] b) throws IOException 

Source Link

Document

Reads data into an array of bytes.

Usage

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

/**
 * Read from compressed file//  w w w  .java  2s  .c  om
 * 
 * @param srcPath
 *            path of compressed file
 * @param fileCompressor
 *            FileCompressor object
 * @throws Exception
 */
@Override
public void read(String srcPath, FileCompressor fileCompressor) throws Exception {
    long t1 = System.currentTimeMillis();
    byte[] data = FileUtil.convertFileToByte(srcPath);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    SevenZFile sevenZFile = new SevenZFile(new File(srcPath));
    try {
        byte[] buffer = new byte[1024];
        int readByte;
        SevenZArchiveEntry entry = sevenZFile.getNextEntry();
        while (entry != null && entry.getSize() > 0) {
            long t2 = System.currentTimeMillis();
            baos = new ByteArrayOutputStream();
            readByte = sevenZFile.read(buffer);
            while (readByte != -1) {
                baos.write(buffer, 0, readByte);
                readByte = sevenZFile.read(buffer);
            }
            BinaryFile binaryFile = new BinaryFile(entry.getName(), baos.toByteArray());
            fileCompressor.addBinaryFile(binaryFile);
            LogUtil.createAddFileLog(fileCompressor, binaryFile, t2, System.currentTimeMillis());
            entry = sevenZFile.getNextEntry();
        }
    } catch (Exception e) {
        FileCompressor.LOGGER.error("Error on get compressor file", e);
    } finally {
        sevenZFile.close();
        baos.close();
    }
    LogUtil.createReadLog(fileCompressor, srcPath, data.length, t1, System.currentTimeMillis());
}

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

public void restore(String source, String outputFolder) {
    byte[] buffer = new byte[1024];

    try {/*from  ww w. ja  v  a2 s  .  c  o m*/

        //create output directory is not exists
        File folder = new File(outputFolder);
        if (!folder.exists()) {
            folder.mkdir();
        }

        //get the zip file content
        SevenZFile sevenZFile = new SevenZFile(new File(source));
        //get the zipped file list entry
        SevenZArchiveEntry ze = sevenZFile.getNextEntry();

        while (ze != null) {

            String fileName = ze.getName();
            File newFile = new File(outputFolder + File.separator + fileName);

            System.out.println("file unzip : " + newFile.getAbsoluteFile());

            //create all non exists folders
            //else you will hit FileNotFoundException for compressed folder
            new File(newFile.getParent()).mkdirs();

            FileOutputStream fos = new FileOutputStream(newFile);

            int len;
            while ((len = sevenZFile.read(buffer)) > 0) {
                fos.write(buffer, 0, len);
            }

            fos.close();
            ze = sevenZFile.getNextEntry();
        }

        sevenZFile.close();

        System.out.println("Done");

    } catch (IOException ex) {
        ex.printStackTrace();
    }
}

From source file:org.codehaus.plexus.archiver.sevenz.SevenZUnArchiver.java

protected void execute() throws ArchiverException {
    File source = getSourceFile();
    File dest = this.getDestDirectory();
    if (source.lastModified() > dest.lastModified()) {
        try {//from  w  w  w  .  j  av a 2 s.c o  m
            SevenZFile sevenZFile = new SevenZFile(source);
            SevenZArchiveEntry entry;
            FileOutputStream fos;
            byte[] bytes = new byte[1024];
            int read;
            long offset = 0;
            long size;
            while ((entry = sevenZFile.getNextEntry()) != null) {
                fos = new FileOutputStream(Paths.get(dest.getAbsolutePath(), entry.getName()).toFile());
                size = entry.getSize();
                do {
                    read = sevenZFile.read(bytes);
                    if (read > 0)
                        fos.write(bytes, 0, read);
                } while (size > offset && read > 0);
                fos.close();
            }
            sevenZFile.close();
        } catch (IOException ioe) {
            throw new ArchiverException(
                    "Problem unzipping 7z file " + source.getAbsolutePath() + " to " + dest.getAbsolutePath(),
                    ioe);
        }
    }
}