Example usage for org.apache.commons.vfs2 FileSystemManager createVirtualFileSystem

List of usage examples for org.apache.commons.vfs2 FileSystemManager createVirtualFileSystem

Introduction

In this page you can find the example usage for org.apache.commons.vfs2 FileSystemManager createVirtualFileSystem.

Prototype

FileObject createVirtualFileSystem(FileObject rootFile) throws FileSystemException;

Source Link

Document

Creates a virtual file system.

Usage

From source file:org.geoserver.backuprestore.utils.BackupUtils.java

/**
 * Extracts the archive file {@code archiveFile} to {@code targetFolder}; both shall previously exist.
 *
 * @param archiveFile//from  w ww .  j a  va  2  s. c o m
 * @param targetFolder
 * @throws IOException
 */
public static void extractTo(Resource archiveFile, Resource targetFolder) throws IOException {
    FileSystemManager manager = VFS.getManager();
    String sourceURI = resolveArchiveURI(archiveFile);

    FileObject source = manager.resolveFile(sourceURI);
    if (manager.canCreateFileSystem(source)) {
        source = manager.createFileSystem(source);
    }
    FileObject target = manager
            .createVirtualFileSystem(manager.resolveFile(targetFolder.dir().getAbsolutePath()));

    FileSelector selector = new AllFileSelector() {
        @Override
        public boolean includeFile(FileSelectInfo fileInfo) {
            LOGGER.fine("Uncompressing " + fileInfo.getFile().getName().getFriendlyURI());
            return true;
        }
    };
    target.copyFrom(source, selector);
    source.close();
    target.close();
    manager.closeFileSystem(source.getFileSystem());
}

From source file:org.geoserver.backuprestore.utils.BackupUtils.java

/**
 * Compress {@code sourceFolder} to the archive file {@code archiveFile}; both shall previously exist.
 * //  www.  java 2  s . c o  m
 * @param sourceFolder
 * @param archiveFile
 * @throws IOException
 */
public static void compressTo(Resource sourceFolder, Resource archiveFile) throws IOException {
    // See https://commons.apache.org/proper/commons-vfs/filesystems.html
    // for the supported filesystems

    FileSystemManager manager = VFS.getManager();

    FileObject sourceDir = manager
            .createVirtualFileSystem(manager.resolveFile(sourceFolder.dir().getAbsolutePath()));

    try {
        if ("zip".equalsIgnoreCase(FileUtils.getExtension(archiveFile.path()))) {
            // apache VFS does not support ZIP as writable FileSystem

            OutputStream fos = archiveFile.out();

            // Create access to zip.
            ZipOutputStream zos = new ZipOutputStream(fos);

            // add entry/-ies.
            for (FileObject sourceFile : sourceDir.getChildren()) {
                writeEntry(zos, sourceFile, null);
            }

            // Close streams
            zos.flush();
            zos.close();
            fos.close();
        } else {
            // Create access to archive.
            FileObject zipFile = manager.resolveFile(resolveArchiveURI(archiveFile));
            zipFile.createFile();
            ZipOutputStream zos = new ZipOutputStream(zipFile.getContent().getOutputStream());

            // add entry/-ies.
            for (FileObject sourceFile : sourceDir.getChildren()) {
                writeEntry(zos, sourceFile, null);
            }

            // Close streams
            zos.flush();
            zos.close();
            zipFile.close();
            manager.closeFileSystem(zipFile.getFileSystem());
        }
    } finally {
        manager.closeFileSystem(sourceDir.getFileSystem());
    }
}