use of org.openide.filesystems.FileObject in project gephi by gephi.
the class ImportUtils method getArchivedFile.
public static FileObject getArchivedFile(final FileObject fileObject) {
if (!isArchiveFile(fileObject)) {
return fileObject;
}
FileObject result = fileObject;
// ZIP and JAR archives
if (FileUtil.isArchiveFile(fileObject)) {
FileObject[] children = FileUtil.getArchiveRoot(fileObject).getChildren();
if (children.length > 0) {
result = children[0];
}
} else {
// GZ or BZIP2 archives
boolean isGz = fileObject.getExt().equalsIgnoreCase("gz");
boolean isBzip = fileObject.getExt().equalsIgnoreCase("bz2");
if (isGz || isBzip) {
try {
String[] splittedFileName = fileObject.getName().split("\\.");
if (splittedFileName.length < 2) {
return fileObject;
}
String fileExt1 = splittedFileName[splittedFileName.length - 1];
String fileExt2 = splittedFileName[splittedFileName.length - 2];
File tempFile;
if (fileExt1.equalsIgnoreCase("tar")) {
String fname = fileObject.getName().replaceAll("\\.tar$", "");
fname = fname.replace(fileExt2, "");
tempFile = File.createTempFile(fname, "." + fileExt2);
// Untar & unzip
if (isGz) {
tempFile = getGzFile(fileObject, tempFile, true);
} else {
tempFile = getBzipFile(fileObject, tempFile, true);
}
} else {
String fname = fileObject.getName();
fname = fname.replace(fileExt1, "");
tempFile = File.createTempFile(fname, "." + fileExt1);
// Unzip
if (isGz) {
tempFile = getGzFile(fileObject, tempFile, false);
} else {
tempFile = getBzipFile(fileObject, tempFile, false);
}
}
tempFile.deleteOnExit();
tempFile = FileUtil.normalizeFile(tempFile);
result = FileUtil.toFileObject(tempFile);
} catch (IOException ex) {
Exceptions.printStackTrace(ex);
}
}
}
if (result == null) {
// Never return null if the archive is empty, broken or anything
result = fileObject;
}
return result;
}
use of org.openide.filesystems.FileObject in project gephi by gephi.
the class ImportControllerImpl method importFile.
@Override
public Container importFile(File file) throws FileNotFoundException {
FileObject fileObject = FileUtil.toFileObject(file);
if (fileObject != null) {
// Unzip and return content file
fileObject = ImportUtils.getArchivedFile(fileObject);
file = FileUtil.toFile(fileObject);
FileImporterBuilder builder = getMatchingImporter(fileObject);
if (fileObject != null && builder != null) {
Container c = importFile(fileObject.getInputStream(), builder.buildImporter(), file);
return c;
}
}
return null;
}
use of org.openide.filesystems.FileObject in project gephi by gephi.
the class SaveTask method run.
@Override
public void run() {
Progress.start(progressTicket);
Progress.setDisplayName(progressTicket, NbBundle.getMessage(SaveTask.class, "SaveTask.name"));
File writeFile = null;
try {
String tempFileName = file.getName() + "_temp" + System.currentTimeMillis();
writeFile = new File(file.getParent(), tempFileName);
FileOutputStream outputStream = null;
ZipOutputStream zipOut = null;
BufferedOutputStream bos = null;
DataOutputStream dos = null;
try {
// Stream
int zipLevel = NbPreferences.forModule(SaveTask.class).getInt(ZIP_LEVEL_PREFERENCE, 9);
outputStream = new FileOutputStream(writeFile);
zipOut = new ZipOutputStream(outputStream);
zipOut.setLevel(zipLevel);
bos = new BufferedOutputStream(zipOut);
dos = new DataOutputStream(bos);
// Providers and workspace
Collection<WorkspacePersistenceProvider> providers = PersistenceProviderUtils.getPersistenceProviders();
Workspace[] workspaces = project.getLookup().lookup(WorkspaceProviderImpl.class).getWorkspaces();
// Setup progress
Progress.switchToDeterminate(progressTicket, 1 + (1 + providers.size()) * workspaces.length);
// Write Project
writeProject(dos, zipOut);
Progress.progress(progressTicket);
// Write Workspace files
for (Workspace ws : workspaces) {
writeWorkspace(ws, dos, zipOut);
Progress.progress(progressTicket);
for (WorkspacePersistenceProvider provider : providers) {
if (provider instanceof WorkspaceXMLPersistenceProvider) {
writeWorkspaceChildrenXML(ws, (WorkspaceXMLPersistenceProvider) provider, dos, zipOut);
} else if (provider instanceof WorkspaceBytesPersistenceProvider) {
writeWorkspaceChildrenBytes(ws, (WorkspaceBytesPersistenceProvider) provider, dos, zipOut);
}
Progress.progress(progressTicket);
if (cancel) {
break;
}
}
if (cancel) {
break;
}
}
Progress.switchToIndeterminate(progressTicket);
zipOut.finish();
} finally {
if (dos != null) {
try {
dos.close();
} catch (IOException ex1) {
}
}
if (bos != null) {
try {
bos.close();
} catch (IOException ex1) {
}
}
if (zipOut != null) {
try {
zipOut.close();
} catch (IOException ex1) {
}
}
if (outputStream != null) {
try {
outputStream.close();
} catch (IOException ex1) {
}
}
}
Progress.finish(progressTicket);
// Rename file
if (!cancel && writeFile.exists()) {
// Delete original file
if (file.exists()) {
file.delete();
}
FileObject tempFileObject = FileUtil.toFileObject(writeFile);
FileLock lock = tempFileObject.lock();
tempFileObject.rename(lock, getFileNameWithoutExt(file), getFileExtension(file));
lock.releaseLock();
}
} catch (Exception ex) {
if (ex instanceof GephiFormatException) {
throw (GephiFormatException) ex;
}
throw new GephiFormatException(SaveTask.class, ex);
} finally {
if (writeFile != null && writeFile.exists()) {
FileObject tempFileObject = FileUtil.toFileObject(writeFile);
try {
tempFileObject.delete();
} catch (IOException ex) {
}
}
}
Progress.finish(progressTicket);
}
use of org.openide.filesystems.FileObject in project Universal-G-Code-Sender by winder.
the class ActionRegistrationService method createAndLocalizeFullMenu.
/**
* Creates a folder path in the netbeans filesystem and sets a localized
* display name or each level of the path.
*/
public FileObject createAndLocalizeFullMenu(String path, String localizedPath) throws IOException {
FileObject root = FileUtil.getConfigRoot();
String[] paths = path.split("/");
String[] names = localizedPath.split("/");
if (paths.length != names.length) {
throw new IllegalArgumentException("Path length must equal localized path length: " + path + ", " + localizedPath);
}
if (!paths[0].equals(names[0])) {
throw new IllegalArgumentException("Path and localized path must be in the same top level directory. Found: " + paths[0] + " and " + names[0]);
}
String fullPath = paths[0];
FileObject in = FileUtil.createFolder(root, fullPath);
for (int i = 1; i < paths.length; i++) {
fullPath = fullPath + "/" + paths[i];
in = FileUtil.createFolder(root, fullPath);
in.setAttribute("displayName", names[i]);
in.refresh();
}
return in;
}
use of org.openide.filesystems.FileObject in project Universal-G-Code-Sender by winder.
the class MacroService method reInitActions.
public void reInitActions() {
String menuPath = "Menu/Machine/Macros";
String actionCategory = "Macro";
String localCategory = Localization.getString("platform.menu.macros");
String localized = String.format("Menu/%s/%s", Localization.getString("platform.menu.machine"), Localization.getString("platform.menu.macros"));
try {
FileObject root = FileUtil.getConfigRoot();
// Clear out the menu items.
FileUtil.createFolder(root, menuPath).delete();
FileUtil.createFolder(root, menuPath);
String actionPath = "/Actions/" + actionCategory;
FileUtil.createFolder(root, actionPath).delete();
// FileObject actionsObject = FileUtil.createFolder(root, actionPath);
ActionRegistrationService ars = Lookup.getDefault().lookup(ActionRegistrationService.class);
BackendAPI backend = CentralLookup.getDefault().lookup(BackendAPI.class);
Settings settings = backend.getSettings();
int numMacros = settings.getNumMacros();
for (int i = 0; i < numMacros; i++) {
Macro m = settings.getMacro(i);
ars.registerAction(MacroAction.class.getCanonicalName() + "." + m.getName(), m.getName(), actionCategory, localCategory, null, menuPath, localized, new MacroAction(settings, backend, i));
}
} catch (Exception e) {
e.printStackTrace();
}
}
Aggregations