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();
}
}
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;
}
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);
}
}
Aggregations