Example usage for org.apache.commons.vfs.impl DefaultFileSystemManager resolveFile

List of usage examples for org.apache.commons.vfs.impl DefaultFileSystemManager resolveFile

Introduction

In this page you can find the example usage for org.apache.commons.vfs.impl DefaultFileSystemManager resolveFile.

Prototype

public FileObject resolveFile(final String uri) throws FileSystemException 

Source Link

Document

Locates a file by URI.

Usage

From source file:com.threecrickets.sincerity.util.IoUtil.java

/**
 * Copies a complete subdirectory tree using Apache Commons VFS.
 * //from w ww .j a  v a  2  s.c o m
 * @param fromDir
 *        The source directory
 * @param toDir
 *        The target directory
 * @throws IOException
 *         In case of an I/O error
 */
public static void copyRecursive(File fromDir, File toDir) throws IOException {
    DefaultFileSystemManager manager = new DefaultFileSystemManager();
    try {
        DefaultLocalFileProvider fileProvider = new DefaultLocalFileProvider();
        manager.addProvider("file", fileProvider);
        manager.setDefaultProvider(fileProvider);
        manager.setFilesCache(new DefaultFilesCache());
        manager.init();
        copyRecursive(manager, manager.resolveFile(fromDir.toURI().toString()),
                manager.resolveFile(toDir.toURI().toString()));
    } finally {
        manager.close();
    }
}

From source file:com.threecrickets.sincerity.util.IoUtil.java

/**
 * Unpacks all files in an archive using Apache Commons VFS.
 * <p>/*  www.ja  v a  2  s.c o  m*/
 * Supported formats: zip, tar.gz/tgz, tar.bz2.
 * 
 * @param archiveFile
 *        The archive file
 * @param destinationDir
 *        The destination directory
 * @param workDir
 *        The work directory
 * @throws IOException
 *         In case of an I/O error
 */
public static void unpack(File archiveFile, File destinationDir, File workDir) throws IOException {
    String scheme = null;
    String name = archiveFile.getName();
    DefaultFileSystemManager manager = new DefaultFileSystemManager();
    try {
        boolean untar = false;
        if (name.endsWith(".zip")) {
            scheme = "zip:";
            manager.addProvider("zip", new ZipFileProvider());
        } else if (name.endsWith(".tar.gz") || name.endsWith(".tgz")) {
            scheme = "gz:";
            untar = true;
            manager.addProvider("tar", new TarFileProvider());
            manager.addProvider("gz", new GzipFileProvider());
            manager.addProvider("tgz", new TgzFileProvider());
        } else if (name.endsWith(".tar.bz2")) {
            scheme = "bz2:";
            untar = true;
            manager.addProvider("tar", new TarFileProvider());
            manager.addProvider("bz2", new Bzip2FileProvider());
            manager.addProvider("tbz2", new Tbz2FileProvider());
        }

        if (scheme != null) {
            DefaultFileReplicator replicator = new DefaultFileReplicator(workDir);
            replicator.init();
            manager.setReplicator(replicator);
            manager.setTemporaryFileStore(replicator);
            DefaultLocalFileProvider fileProvider = new DefaultLocalFileProvider();
            manager.addProvider("file", fileProvider);
            manager.setDefaultProvider(fileProvider);
            manager.setFilesCache(new DefaultFilesCache());
            manager.init();

            String path = scheme + archiveFile.toURI();
            FileObject fileObject = manager.resolveFile(path);
            FileObject[] children = fileObject.getChildren();
            if (untar && children.length > 0) {
                FileObject tar = manager
                        .resolveFile(new File(workDir, children[0].getName().getBaseName()).toURI().toString());
                org.apache.commons.vfs.FileUtil.copyContent(children[0], tar);
                tar = manager.resolveFile("tar:" + tar.getName());
                children = tar.getChildren();
            }

            for (FileObject child : children)
                copyRecursive(manager, child, manager.resolveFile(destinationDir.toURI().toString()));
        }
    } finally {
        manager.close();
    }
}

From source file:egovframework.rte.fdl.filehandling.FilehandlingServiceTest.java

/**
 * ? ? , ?  ??  ?? ?//from  www. j a  v a2s .com
 * ?  ? ? ? .
 * @throws Exception
 */
@Test
public void testCaching() throws Exception {
    String testFolder = "d:/workspace/java/e-gov/eGovFramework/RTE/DEV/trunk/Foundation/egovframework.rte.fdl.filehandling/testfolder";
    FileSystemManager manager = VFS.getManager();

    FileObject scratchFolder = manager.resolveFile(testFolder);

    // testfolder ?  ? 
    scratchFolder.delete(Selectors.EXCLUDE_SELF);

    // ? Manager ?
    DefaultFileSystemManager fs = new DefaultFileSystemManager();
    fs.setFilesCache(manager.getFilesCache());

    // zip, jar, tgz, tar, tbz2, file
    if (!fs.hasProvider("file")) {
        fs.addProvider("file", new DefaultLocalFileProvider());
    }

    fs.setCacheStrategy(CacheStrategy.ON_RESOLVE);
    fs.init();

    // ? ? ?
    FileObject foBase2 = fs.resolveFile(testFolder);
    log.debug("## scratchFolder.getName().getPath() : " + scratchFolder.getName().getPath());

    FileObject cachedFolder = foBase2.resolveFile(scratchFolder.getName().getPath());

    // ??  ?
    FileObject[] fos = cachedFolder.getChildren();
    assertFalse(contains(fos, "file1.txt"));

    // ??
    scratchFolder.resolveFile("file1.txt").createFile();

    // ? 
    // BUT cachedFolder ? ??  ?
    fos = cachedFolder.getChildren();
    assertFalse(contains(fos, "file1.txt"));

    // 
    cachedFolder.refresh();
    // ?? 
    fos = cachedFolder.getChildren();
    assertTrue(contains(fos, "file1.txt"));

}