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

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

Introduction

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

Prototype

public void setName(final String name) 

Source Link

Document

Set this entry's name.

Usage

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

/**
 * Adds an empty directory to the archive.
 * /*ww  w  .ja v a  2  s .c  o m*/
 * @param dir
 *            empty directory
 * @throws IOException
 */
private void addEmptyDir(File dir) throws IOException {
    SevenZArchiveEntry entry = new SevenZArchiveEntry();
    entry.setName(dir.getAbsolutePath().substring(baseDir.length()).replace('\\', '/'));
    entry.setAccessDate(dir.lastModified());
    entry.setCreationDate(dir.lastModified());
    entry.setLastModifiedDate(dir.lastModified());
    entry.setDirectory(true);
    sevenZOutput.putArchiveEntry(entry);
    sevenZOutput.closeArchiveEntry();

}

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

/**
 * Adds a file to the archive/*from   w  ww  .  j a  v  a2  s  .c  o m*/
 * 
 * @param file
 *            file to be compressed
 * @throws IOException
 */
private void addFile(File file) throws IOException {
    if (!exclude.isEmpty()) {
        String extension = getFileExtension(file);
        if (!extension.isEmpty() && exclude.contains(extension.toLowerCase())) {
            return;
        }
    }

    SevenZArchiveEntry entry = new SevenZArchiveEntry();
    entry.setName(file.getAbsolutePath().substring(baseDir.length()).replace('\\', '/'));
    entry.setAccessDate(file.lastModified());
    entry.setCreationDate(file.lastModified());
    entry.setLastModifiedDate(file.lastModified());
    entry.setDirectory(false);

    sevenZOutput.putArchiveEntry(entry);

    FileInputStream is = new FileInputStream(file);
    long size = file.length();
    byte[] buffer = new byte[BUFFER_SIZE];
    boolean setCompressMethod = false;
    while (size > 0) {
        long read = Math.min(buffer.length, size);
        is.read(buffer, 0, (int) read);
        if (!setCompressMethod) {
            setCompressMethod = true;
            entry.setContentMethods(Arrays.asList(new SevenZMethodConfiguration(
                    Export.findBestCompressionMethod(buffer, compressionMehtod))));
        }
        sevenZOutput.write(buffer, 0, (int) read);
        size -= read;
    }
    is.close();
    sevenZOutput.closeArchiveEntry();
}

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

/**
 * Creates a 7z file archive entry./*from w w w .j av  a 2  s .c  o  m*/
 * 
 * @param path
 *            archive path
 * @param date
 *            file creation date
 * @return {@code SevenZArchiveEntry}
 */
private SevenZArchiveEntry createFileEntry(String path, Date date) {
    SevenZArchiveEntry entry = new SevenZArchiveEntry();
    entry.setName(path);
    entry.setAccessDate(date);
    entry.setCreationDate(date);
    entry.setLastModifiedDate(date);
    entry.setDirectory(false);
    return entry;
}

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

/**
 * Creates a 7z directory archive entry.
 * /*from  w w  w  . j  a  v a  2s .c  om*/
 * @param directory
 *            archive directory path
 * @param date
 *            directory creation date
 * @throws IOException
 */
private void createDirEntry(String directory, Date date) throws IOException {
    SevenZArchiveEntry entry = new SevenZArchiveEntry();
    entry.setName(directory);
    entry.setAccessDate(date);
    entry.setCreationDate(date);
    entry.setLastModifiedDate(date);
    entry.setDirectory(true);
    sevenZOutput.putArchiveEntry(entry);
    sevenZOutput.closeArchiveEntry();
}

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

/**
 * Dumps all users to the archive./*from  ww w.j  a v a2 s .  co  m*/
 * 
 * @param dbInfo
 *            database information
 * @param privacy
 *            privacy export filter
 * @throws Exception
 */
private void dumpUser(DBexportInfo dbInfo, EnumSet<User.PRIVACY> privacy) throws Exception {
    String archivePath = ROOT_DIR + User.class.getSimpleName() + "/";
    createDirEntry(archivePath, date);
    Iterator iter = new Iterator(dao, User.class, null, null, entityFetchThreshold);
    int blockCounter = 0;
    switch (serialization) {
    case JSON:
        gson = new Gson();
        break;
    case XML:
        xstream = new XStream();
        break;
    default:
        break;
    }
    int counter = 0;

    while (iter.hasNext()) {
        List<Object> l = iter.next();
        ArrayList<Object> newList = new ArrayList<Object>();
        for (Object o : l) {
            User u = (User) o;
            // user must be member of the domain and ACTIVE or deleted
            if (userIDs.contains(u.getDBid())
                    && (u.getHistStatus() == STATUS.ACTIVE || u.getHistStatus() == STATUS.DELETED)) {
                User clone = (User) u.clone();
                UpdateMap map = new UpdateMap(User.Fields.class);
                map.addString(User.Fields.password, null);
                // apply privacy filter
                if (privacy == null || !privacy.contains(User.PRIVACY.NAME)) {
                    Pseudonym fName = Pseudonym.generatePseudonym(Gender.RANDOM);
                    map.addString(User.Fields.firstName, fName.getFirstName());
                    map.addString(User.Fields.lastName, fName.getLastName());
                    map.addString(User.Fields.displayName, fName.getFirstName() + " " + fName.getLastName());
                    map.addString(User.Fields.nickName, null);
                    map.addString(User.Fields.lastName, null);
                    map.addString(User.Fields.email, fName.getEmail());
                }
                if (privacy == null || !privacy.contains(User.PRIVACY.PHONE)) {
                    map.addString(User.Fields.phone, null);
                }
                if (privacy == null || !privacy.contains(User.PRIVACY.MOBILE)) {
                    map.addString(User.Fields.mobile, null);
                }
                if (privacy == null || !privacy.contains(User.PRIVACY.USERID)) {
                    map.addString(User.Fields.userId, null);
                }
                clone.simpleUpdate(map, true);
                // exported user is VIRTUAL
                clone.setHistStatus(STATUS.VIRTUAL);
                clone.setCreatedBy(0);
                clone.setModifiedBy(0);
                newList.add(clone);
                ++counter;
            }
        }
        l = newList;
        SevenZArchiveEntry entry = new SevenZArchiveEntry();
        entry = new SevenZArchiveEntry();
        entry.setName(archivePath + blockCounter++);
        entry.setAccessDate(date);
        entry.setCreationDate(date);
        entry.setLastModifiedDate(date);
        entry.setDirectory(false);
        sevenZOutput.putArchiveEntry(entry);
        byte[] byteStream = null;
        switch (serialization) {
        case JSON:
            byteStream = gson.toJson(l).getBytes();
            break;
        case XML:
            byteStream = xstream.toXML(l).getBytes();
            break;
        case BINARY:
            ByteArrayOutputStream bs = new ByteArrayOutputStream();
            ObjectOutputStream out = new ObjectOutputStream(bs);
            out.writeObject(l);
            out.close();
            byteStream = bs.toByteArray();
            break;
        }
        sevenZOutput.write(byteStream);
        sevenZOutput.closeArchiveEntry();
    }
    dbInfo.addEntityCount(User.class.getSimpleName() + ":" + counter);
}

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

/**
 * Dumps all entities of the database, or just all entities of a domain to
 * the archive.//from w w w .j  av a  2s.  c om
 * 
 * @param clazz
 *            entity to be dumped
 * @param domain
 *            optional domain, or {@code null} for dumping all entities of
 *            the database to the archive
 * @param backupType
 *            type of the backup
 * @throws Exception
 */
private void dumpClass(DBexportInfo dbInfo, Class<?> clazz, Domain domain, DBexportInfo.BACKUP_TYPE backupType)
        throws Exception {
    String archivePath = ROOT_DIR + clazz.getSimpleName() + "/";
    createDirEntry(archivePath, date);

    Iterator iter = new Iterator(dao, clazz, domain, status, entityFetchThreshold);
    int blockCounter = 0;
    switch (serialization) {
    case JSON:
        gson = new Gson();
        break;
    case XML:
        xstream = new XStream();
        break;
    default:
        break;
    }
    dbInfo.addEntityCount(clazz.getSimpleName() + ":" + iter.getEntitiesNum());
    ArrayList<Field> fieldList = new ArrayList<Field>();
    boolean userFields = false;
    if (backupType == DBexportInfo.BACKUP_TYPE.DOMAIN) {
        userFields = collectUserFields(clazz, fieldList);
        if (clazz.getSuperclass() != null) {
            collectUserFields(clazz.getSuperclass(), fieldList);
        }
    }
    if (fieldList.size() > 0) {
        userFields = true;
    }

    boolean singleStep = false;
    if (clazz.equals(CIblob.class)) {
        singleStep = true;
    }

    while (iter.hasNext()) {
        List<Object> l = null;
        if (!singleStep) {
            l = iter.next();
            l = detachBinaryData(dao, clazz, l);
        } else {
            ArrayList<Object> list = new ArrayList<Object>();
            for (int i = 0; i < entityFetchThreshold; ++i) {
                l = iter.nextObject();
                if (l == null) {
                    break;
                }
                l = detachBinaryData(dao, clazz, l);
                list.add(l.get(0));
            }
            if (list.size() == 0) {
                continue;
            }
            l = list;
        }

        SevenZArchiveEntry entry = new SevenZArchiveEntry();
        entry = new SevenZArchiveEntry();
        entry.setName(archivePath + blockCounter++);
        entry.setAccessDate(date);
        entry.setCreationDate(date);
        entry.setLastModifiedDate(date);
        entry.setDirectory(false);
        sevenZOutput.putArchiveEntry(entry);
        byte[] byteStream = null;
        switch (serialization) {
        case JSON:
            byteStream = gson.toJson(l).getBytes();
            break;
        case XML:
            byteStream = xstream.toXML(l).getBytes();
            break;
        case BINARY:
            ByteArrayOutputStream bs = new ByteArrayOutputStream();
            ObjectOutputStream out = new ObjectOutputStream(bs);
            out.writeObject(l);
            out.close();
            byteStream = bs.toByteArray();
            break;
        }
        // collect all user references
        if (userFields) {
            for (Object o : l) {
                for (Field f : fieldList) {
                    f.setAccessible(true);
                    userIDs.add(f.getInt(o));
                }
            }
        }
        sevenZOutput.write(byteStream);
        sevenZOutput.closeArchiveEntry();
        if (l instanceof CIblob) {
            if (dao.isJPA() && dao.getJPAimpl() == DAO.JPA_IMPL.ECLIPSELINK) {
                dao.clear();
            } else {
                dao.detach(l);
            }
            ((CIblob) l).resetBlob();
        }
        l = null;

    }
    iter.close();
}

From source file:org.apache.ant.compress.taskdefs.SevenZ.java

public SevenZ() {
    setFactory(new SevenZStreamFactory() {
        public ArchiveOutputStream getArchiveOutputStream(File f, String encoding) throws IOException {
            SevenZArchiveOutputStream o = (SevenZArchiveOutputStream) super.getArchiveOutputStream(f, encoding);
            if (contentCompression != null) {
                o.setContentCompression(asMethod(contentCompression));
            }/*from w w w .  j  a  va2s  .  com*/
            if (contentMethods != null) {
                o.setContentMethods(contentMethods);
            }
            return o;
        }
    });
    setEntryBuilder(new ArchiveBase.EntryBuilder() {
        public ArchiveEntry buildEntry(ArchiveBase.ResourceWithFlags r) {
            SevenZArchiveEntry entry = new SevenZArchiveEntry();
            entry.setName(r.getName());
            entry.setDirectory(r.getResource().isDirectory());
            entry.setLastModifiedDate(new Date(r.getResource().getLastModified()));
            entry.setSize(r.getResource().getSize());
            if (keepCompression && r.getResourceFlags().hasContentMethods()) {
                entry.setContentMethods(r.getResourceFlags().getContentMethods());
            }
            return entry;
        }
    });
    setFileSetBuilder(new ArchiveBase.FileSetBuilder() {
        public ArchiveFileSet buildFileSet(Resource dest) {
            ArchiveFileSet afs = new SevenZFileSet();
            afs.setSrcResource(dest);
            return afs;
        }
    });
}

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

/**
 * perform the BZip2 compression operation.
 */// w  w  w. ja  v a 2s .  c  o m
public void compress() throws ArchiverException {
    try {
        zOut = new SevenZOutputFile(getDestFile());
        PlexusIoResource source = getSource();
        SevenZArchiveEntry entry = new SevenZArchiveEntry();
        entry.setName(source.getName());
        entry.setDirectory(source.isDirectory());
        zOut.putArchiveEntry(entry);

        InputStream inputStream = source.getContents();
        int read = 0;
        byte[] bytes = new byte[1024];
        while ((read = inputStream.read(bytes)) != -1) {
            zOut.write(bytes, 0, read);
        }
        zOut.closeArchiveEntry();
    } catch (IOException ioe) {
        String msg = "Problem creating 7z " + ioe.getMessage();
        throw new ArchiverException(msg, ioe);
    }
}