Search in sources :

Example 16 with AllFileSelector

use of org.apache.commons.vfs2.AllFileSelector in project javautils by jiadongpo.

the class ZipfileUnpacker method unpack.

// @Override
public void unpack(final File outputDir) throws IOException {
    outputDir.mkdirs();
    final FileObject packFileObject = fileSystemManager.resolveFile(packLocation.toString());
    try {
        final FileObject zipFileSystem = fileSystemManager.createFileSystem(packFileObject);
        try {
            fileSystemManager.toFileObject(outputDir).copyFrom(zipFileSystem, new AllFileSelector());
        } finally {
            zipFileSystem.close();
        }
    } finally {
        packFileObject.close();
    }
}
Also used : AllFileSelector(org.apache.commons.vfs2.AllFileSelector) FileObject(org.apache.commons.vfs2.FileObject)

Example 17 with AllFileSelector

use of org.apache.commons.vfs2.AllFileSelector in project artisynth_core by artisynth.

the class FileCacher method cache.

public File cache(URIx uri, File cacheFile, FileTransferMonitor monitor) throws FileSystemException {
    // For atomic operation, first download to temporary directory
    File tmpCacheFile = new File(cacheFile.getAbsolutePath() + TMP_EXTENSION);
    URIx cacheURI = new URIx(cacheFile.getAbsoluteFile());
    URIx tmpCacheURI = new URIx(tmpCacheFile.getAbsoluteFile());
    FileObject localTempFile = manager.resolveFile(tmpCacheURI.toString(true));
    FileObject localCacheFile = manager.resolveFile(cacheURI.toString(true));
    // will resolve next
    FileObject remoteFile = null;
    // loop through authenticators until we either succeed or cancel
    boolean cancel = false;
    while (remoteFile == null && cancel == false) {
        remoteFile = resolveRemote(uri);
    }
    if (remoteFile == null || !remoteFile.exists()) {
        throw new FileSystemException("Cannot find remote file <" + uri.toString() + ">", new FileNotFoundException("<" + uri.toString() + ">"));
    }
    // monitor the file transfer progress
    if (monitor != null) {
        monitor.monitor(localTempFile, remoteFile, -1, cacheFile.getName());
        monitor.start();
        monitor.fireStartEvent(localTempFile);
    }
    // transfer content
    try {
        if (remoteFile.isFile()) {
            localTempFile.copyFrom(remoteFile, Selectors.SELECT_SELF);
        } else if (remoteFile.isFolder()) {
            // final FileObject fileSystem = manager.createFileSystem(remoteFile);
            localTempFile.copyFrom(remoteFile, new AllFileSelector());
        // fileSystem.close();
        }
        if (monitor != null) {
            monitor.fireCompleteEvent(localTempFile);
        }
    } catch (Exception e) {
        // try to delete local file
        localTempFile.delete();
        throw new RuntimeException("Failed to complete transfer of " + remoteFile.getURL() + " to " + localTempFile.getURL(), e);
    } finally {
        // close files if we need to
        localTempFile.close();
        remoteFile.close();
        if (monitor != null) {
            monitor.release(localTempFile);
            monitor.stop();
        }
    }
    // now that the copy is complete, do a rename operation
    try {
        if (tmpCacheFile.isDirectory()) {
            SafeFileUtils.moveDirectory(tmpCacheFile, cacheFile);
        } else {
            SafeFileUtils.moveFile(tmpCacheFile, cacheFile);
        }
    } catch (Exception e) {
        // delete if possible
        localCacheFile.delete();
        throw new RuntimeException("Failed to atomically move " + "to " + localCacheFile.getURL(), e);
    }
    return cacheFile;
}
Also used : FileSystemException(org.apache.commons.vfs2.FileSystemException) AllFileSelector(org.apache.commons.vfs2.AllFileSelector) FileNotFoundException(java.io.FileNotFoundException) URIx(maspack.fileutil.uri.URIx) FileObject(org.apache.commons.vfs2.FileObject) File(java.io.File) FileSystemException(org.apache.commons.vfs2.FileSystemException) FileNotFoundException(java.io.FileNotFoundException) JSchException(com.jcraft.jsch.JSchException)

Example 18 with AllFileSelector

use of org.apache.commons.vfs2.AllFileSelector in project spoofax by metaborg.

the class ResourceService method localFile.

@Override
public File localFile(FileObject resource, FileObject dir) {
    if (resource instanceof LocalFile) {
        return FileUtils.toFile(resource);
    }
    final File localDir = localPath(dir);
    if (localDir == null) {
        throw new MetaborgRuntimeException("Replication directory " + dir + " is not on the local filesystem, cannot get local file for " + resource);
    }
    try {
        dir.createFolder();
        final FileObject copyLoc;
        if (resource.getType() == FileType.FOLDER) {
            copyLoc = dir;
        } else {
            copyLoc = dir.resolveFile(resource.getName().getBaseName());
        }
        copyLoc.copyFrom(resource, new AllFileSelector());
        return localDir;
    } catch (FileSystemException e) {
        throw new MetaborgRuntimeException("Could not get local file for " + resource, e);
    }
}
Also used : MetaborgRuntimeException(org.metaborg.core.MetaborgRuntimeException) LocalFile(org.apache.commons.vfs2.provider.local.LocalFile) FileSystemException(org.apache.commons.vfs2.FileSystemException) AllFileSelector(org.apache.commons.vfs2.AllFileSelector) FileObject(org.apache.commons.vfs2.FileObject) File(java.io.File) LocalFile(org.apache.commons.vfs2.provider.local.LocalFile)

Aggregations

AllFileSelector (org.apache.commons.vfs2.AllFileSelector)18 FileObject (org.apache.commons.vfs2.FileObject)18 IOException (java.io.IOException)9 FileSelectInfo (org.apache.commons.vfs2.FileSelectInfo)9 KettleDatabaseException (org.pentaho.di.core.exception.KettleDatabaseException)7 KettleException (org.pentaho.di.core.exception.KettleException)7 KettleXMLException (org.pentaho.di.core.exception.KettleXMLException)7 FileSystemException (org.apache.commons.vfs2.FileSystemException)4 File (java.io.File)3 JSchException (com.jcraft.jsch.JSchException)2 FileNotFoundException (java.io.FileNotFoundException)2 InputStream (java.io.InputStream)1 OutputStream (java.io.OutputStream)1 URL (java.net.URL)1 ArrayList (java.util.ArrayList)1 Matcher (java.util.regex.Matcher)1 Pattern (java.util.regex.Pattern)1 Nullable (javax.annotation.Nullable)1 URIx (maspack.fileutil.uri.URIx)1 FileSystemManager (org.apache.commons.vfs2.FileSystemManager)1