Example usage for org.apache.commons.compress.archivers.sevenz SevenZArchiveEntry getSize

List of usage examples for org.apache.commons.compress.archivers.sevenz SevenZArchiveEntry getSize

Introduction

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

Prototype

@Override
public long getSize() 

Source Link

Document

Get this entry's file size.

Usage

From source file:at.treedb.util.SevenZip.java

/**
 * Extracts some data (files and directories) from the archive without
 * extracting the whole archive./*from  ww  w.j a  v a 2 s.  co m*/
 * 
 * @param archive
 *            7-zip archive
 * @param fileList
 *            file extraction list, a path with an ending '/' denotes a
 *            directory
 * @return file list as a map file name/file data
 * @throws IOException
 */
public static HashMap<String, byte[]> exctact(File archive, String... fileList) throws IOException {
    HashSet<String> fileSet = new HashSet<String>();
    ArrayList<String> dirList = new ArrayList<String>();
    for (String f : fileList) {
        if (!f.endsWith("/")) {
            fileSet.add(f);
        } else {
            dirList.add(f);
        }
    }
    HashMap<String, byte[]> resultMap = new HashMap<String, byte[]>();
    SevenZFile sevenZFile = new SevenZFile(archive);
    do {
        SevenZArchiveEntry entry = sevenZFile.getNextEntry();
        if (entry == null) {
            break;
        }
        // convert window path to unix style
        String name = entry.getName().replace('\\', '/');
        if (!entry.isDirectory()) {
            boolean storeFile = false;
            if (fileSet.contains(name)) {
                storeFile = true;
            } else {
                // search directories
                for (String s : dirList) {
                    if (name.startsWith(s)) {
                        storeFile = true;
                        break;
                    }
                }
            }
            // store the file
            if (storeFile) {
                int size = (int) entry.getSize();
                byte[] data = new byte[size];
                sevenZFile.read(data, 0, size);
                resultMap.put(name, data);
                // in this case we can finish the extraction loop
                if (dirList.isEmpty() && resultMap.size() == fileSet.size()) {
                    break;
                }
            }
        }
    } while (true);
    sevenZFile.close();
    return resultMap;
}

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

/**
 * Read from compressed file//  w  w w. j av a  2s  .com
 * 
 * @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:es.ucm.fdi.util.archive.SevenZipFormat.java

public void expand(File source, File destDir) throws IOException {
    assertIs7Zip(source);//  www  .ja  v a2 s  . c o m

    try (SevenZFile zf = new SevenZFile(source)) {
        log.info("Extracting 7zip: " + source.getName());

        while (true) {
            SevenZArchiveEntry e = zf.getNextEntry();
            if (e == null)
                return;

            String name = FileUtils.toCanonicalPath(e.getName());
            log.info(" - processing 7zip entry: " + name + " - " + e.getSize());

            if (e.isDirectory()) {
                //log.debug("\tExtracting directory "+e.getName());
                File dir = new File(destDir, name);
                dir.mkdirs();
                continue;
            }

            //log.debug("\tExtracting file "+name);
            File outFile = new File(destDir, name);
            if (!outFile.getParentFile().exists()) {
                //log.warn("weird 7z: had to create parent: "+outFile.getParentFile());
                outFile.getParentFile().mkdirs();
            }

            try (FileOutputStream fos = new FileOutputStream(outFile)) {
                byte[] b = new byte[(int) e.getSize()];
                zf.read(b);
                fos.write(b);
            }
        }
    }
}

From source file:at.treedb.util.SevenZip.java

/**
 * Constructor/*from www  . j a v  a  2 s  .  com*/
 * 
 * @param file
 *            7-zip archive file
 * @param iface
 *            interface to display the extraction progress
 * @throws IOException
 */
public SevenZip(File file, DomainInstallationIface iface) throws Exception {
    sevenZFile = new SevenZFile(file);
    entryMap = new HashMap<String, ArchiveEntry>();
    // create a temporary file containing the extracted archive
    dumpFile = FileStorage.getInstance().createTempFile("sevenZip", ".dump");
    FileOutputStream writer = new FileOutputStream(dumpFile);
    byte[] readBuffer = new byte[READ_BUFFER];
    int offset = 0;
    if (iface != null) {
        iface.writeInternalMessage(InstallationProgress.Type.MSG,
                "Extracting installation package: '.' stands for " + READ_BUFFER + " MB data");
        iface.writeInternalMessage(null, "<br>");
    }
    int count = 0;
    int bytesWritten = 0;
    // extract and dump the files
    do {
        SevenZArchiveEntry entry = sevenZFile.getNextEntry();
        if (entry == null) {
            break;
        }
        if (!entry.isDirectory()) {
            int readBytes = (int) entry.getSize();
            int size = readBytes;
            int index = 0;
            while (readBytes > 0) {
                int read = Math.min(readBytes, READ_BUFFER);
                bytesWritten += read;
                if (iface != null) {
                    iface.writeInternalMessage(null, "<b>.</b>");
                    ++count;
                    if (count == 80) {
                        iface.writeInternalMessage(null, "<br>");
                        count = 0;
                    }
                }
                sevenZFile.read(readBuffer, index, read);
                writer.write(readBuffer, 0, read);
                readBytes -= read;
            }
            entryMap.put(entry.getName().replace('\\', '/'), new ArchiveEntry(offset, size));
            offset += size;
        }
    } while (true);
    if (iface != null) {
        iface.writeInternalMessage(null, "<br>");
        iface.writeInternalMessage(InstallationProgress.Type.MSG,
                "Extraction finished: " + bytesWritten + " bytes written");
    }
    writer.close();
    dumpReader = new FileInputStream(dumpFile);
    channel = dumpReader.getChannel();
}

From source file:com.izforge.izpack.util.compress.SevenZArchiveInputStream.java

@Override
public ArchiveEntry getNextEntry() throws IOException {
    final SevenZArchiveEntry sevenZArchiveEntry = zFile.getNextEntry();

    currentEntry = sevenZArchiveEntry;/*from   www. ja  v a  2 s.c  o m*/
    currentBytesRead = 0;

    if (sevenZArchiveEntry != null) {
        return new ArchiveEntry() {
            @Override
            public String getName() {
                return sevenZArchiveEntry.getName();
            }

            @Override
            public long getSize() {
                return sevenZArchiveEntry.getSize();
            }

            @Override
            public boolean isDirectory() {
                return sevenZArchiveEntry.isDirectory();
            }

            @Override
            public Date getLastModifiedDate() {
                return sevenZArchiveEntry.getLastModifiedDate();
            }
        };
    }

    return null;
}

From source file:at.treedb.backup.Import.java

/**
 * Reads a archive entry./* w  ww .  jav a2  s.  com*/
 * 
 * @param path
 *            archive path
 * @return archive entry data
 * @throws IOException
 */
private byte[] readData(String path) throws IOException {
    if (!archiveMap.containsKey(path)) {
        return null;
    }
    byte[] content = null;
    sevenZFile = new SevenZFile(new File(archivePath));
    while (true) {
        SevenZArchiveEntry entry = sevenZFile.getNextEntry();
        if (entry == null) {
            break;
        }
        if (entry.isDirectory()) {
            continue;
        }
        if (entry.getName().equals(path)) {
            int size = (int) entry.getSize();
            content = new byte[size];
            int index = 0;
            while (size > 0) {
                int read = sevenZFile.read(content, index, size);
                size -= read;
                index += read;
            }
            break;
        }
    }
    sevenZFile.close();
    return content;
}

From source file:at.treedb.backup.Import.java

public void writeFile(DAOiface dao, DBfile file, String path) throws Exception {
    if (!archiveMap.containsKey(path)) {
        throw new Exception("Import.writeFile(): path not found - " + path);
    }//w w  w . j  a v  a  2  s  .c  o  m
    byte[] content = null;
    sevenZFile = new SevenZFile(new File(archivePath));
    while (true) {
        SevenZArchiveEntry entry = sevenZFile.getNextEntry();
        if (entry == null) {
            break;
        }
        if (entry.isDirectory()) {
            continue;
        }
        if (entry.getName().equals(path)) {
            int size = (int) entry.getSize();
            content = new byte[Math.min(Export.BUFFER_SIZE, size)];
            int read = content.length;
            DBoutputStream os = new DBoutputStream(file, dao);
            while (size > 0) {
                int readCount = sevenZFile.read(content, 0, read);
                size -= readCount;
                read = Math.min(size, read);
                os.write(content, 0, readCount);
            }
            os.close();
            break;
        }
    }
    sevenZFile.close();
}

From source file:es.ucm.fdi.util.archive.SevenZipFormat.java

public boolean extractOne(File source, String path, File dest) throws IOException {
    assertIs7Zip(source);//from  ww  w  .  java 2 s . c  o m

    try (SevenZFile zf = new SevenZFile(source)) {
        for (SevenZArchiveEntry e : zf.getEntries()) {
            String name = FileUtils.toCanonicalPath(e.getName());
            // System.err.println(" "+name+" =? "+path);
            if (!name.equals(path) || e.isDirectory()) {
                continue;
            }

            if (!dest.getParentFile().exists()) {
                //log.warn("weird 7z: had to create parent: "+outFile.getParentFile());
                dest.getParentFile().mkdirs();
            }

            try (FileOutputStream fos = new FileOutputStream(dest)) {
                byte[] b = new byte[(int) e.getSize()];
                zf.read(b);
                return true;
            }
        }
    }
    return false;
}

From source file:com.github.n_i_e.dirtreedb.SevenZipListerForFile.java

@Override
protected PathEntry getNext() throws IOException {
    SevenZArchiveEntry z = sevenzfile.getNextEntry();
    if (z == null) {
        return null;
    }/*from  w  w  w. j av  a2s .  c o  m*/
    int newtype = z.isDirectory() ? PathEntry.COMPRESSEDFOLDER : PathEntry.COMPRESSEDFILE;
    String s = z.getName();
    if (s == null) {
        s = AbstractCompressorLister.getBasename(getBasePath());
    }
    ;
    s = s.replace("\\", "/");
    if (z.isDirectory() && !s.endsWith("/")) {
        s = s + "/";
    }
    PathEntry next_entry = new PathEntry(getBasePath().getPath() + "/" + s, newtype);
    next_entry.setDateLastModified(z.getLastModifiedDate().getTime());
    next_entry.setStatus(PathEntry.DIRTY);
    next_entry.setSize(z.getSize());
    next_entry.setCompressedSize(z.getSize());
    if (isCsumRequested() && newtype == PathEntry.COMPRESSEDFILE) {
        next_entry.setCsum(new SevenZipInputStream());
    }
    if (next_entry.getSize() < 0) {
        next_entry.setSize(0);
    }
    if (next_entry.getCompressedSize() < 0) {
        next_entry.setCompressedSize(next_entry.getSize());
    }
    return next_entry;
}

From source file:com.globalsight.everest.webapp.pagehandler.administration.createJobs.CreateJobsMainHandler.java

/**
 * Add a progress bar for each files within a 7z file.
 * @param file//from w ww.  j ava  2s. co m
 * @throws Exception 
 */
private String addZip7zFile(File file) throws Exception {
    String zip7zFileFullPath = file.getPath();
    String zip7zFilePath = zip7zFileFullPath.substring(0, zip7zFileFullPath.indexOf(file.getName()));

    List<SevenZArchiveEntry> entriesInZip7z = CreateJobUtil.getFilesIn7zFile(file);

    StringBuffer ret = new StringBuffer("");
    for (SevenZArchiveEntry item : entriesInZip7z) {
        if (ret.length() > 0) {
            ret.append(",");
        }
        String zip7zEntryName = item.getName();
        /*
         * The unzipped files are in folders named by the zip file name
         */
        String unzippedFileFullPath = zip7zFilePath
                + file.getName().substring(0, file.getName().lastIndexOf(".")) + "_"
                + CreateJobUtil.getFileExtension(file) + File.separator + zip7zEntryName;
        // if zip file contains subf,olders, entry name will contains "/" or
        // "\"
        if (zip7zEntryName.indexOf("/") != -1) {
            zip7zEntryName = zip7zEntryName.substring(zip7zEntryName.lastIndexOf("/") + 1);
        } else if (zip7zEntryName.indexOf("\\") != -1) {
            zip7zEntryName = zip7zEntryName.substring(zip7zEntryName.lastIndexOf("\\") + 1);
        }
        String id = CreateJobUtil.getFileId(unzippedFileFullPath);
        ret.append("{id:'").append(id).append("',zipName:'").append(file.getName().replace("'", "\\'"))
                .append("',path:'")
                .append(unzippedFileFullPath.replace("\\", File.separator).replace("/", File.separator)
                        .replace("\\", "\\\\").replace("'", "\\'"))
                .append("',name:'").append(zip7zEntryName.replace("'", "\\'")).append("',size:'")
                .append(item.getSize()).append("'}");
    }
    return ret.toString();
}