use of net.sf.mzmine.modules.rawdatamethods.rawdataimport.RawDataFileType in project mzmine2 by mzmine.
the class ZipReadTask method run.
/**
* @see java.lang.Runnable#run()
*/
public void run() {
// Update task status
setStatus(TaskStatus.PROCESSING);
logger.info("Started opening compressed file " + file);
try {
// Name of the uncompressed file
String newName = file.getName();
if (newName.toLowerCase().endsWith(".zip") || newName.toLowerCase().endsWith(".gz")) {
newName = FilenameUtils.removeExtension(newName);
}
// Create decompressing stream
FileInputStream fis = new FileInputStream(file);
InputStream is;
long decompressedSize = 0;
switch(fileType) {
case ZIP:
ZipInputStream zis = new ZipInputStream(fis);
ZipEntry entry = zis.getNextEntry();
newName = entry.getName();
decompressedSize = entry.getSize();
if (decompressedSize < 0)
decompressedSize = 0;
is = zis;
break;
case GZIP:
is = new GZIPInputStream(fis);
// Ballpark a
decompressedSize = (long) (file.length() * 1.5);
// progress
if (decompressedSize < 0)
decompressedSize = 0;
break;
default:
setErrorMessage("Cannot decompress file type: " + fileType);
setStatus(TaskStatus.ERROR);
return;
}
tmpDir = Files.createTempDir();
tmpFile = new File(tmpDir, newName);
logger.finest("Decompressing to file " + tmpFile);
tmpFile.deleteOnExit();
tmpDir.deleteOnExit();
FileOutputStream ous = new FileOutputStream(tmpFile);
// Decompress the contents
copy = new StreamCopy();
copy.copy(is, ous, decompressedSize);
// Close the streams
is.close();
ous.close();
if (isCanceled())
return;
// Find the type of the decompressed file
RawDataFileType fileType = RawDataFileTypeDetector.detectDataFileType(tmpFile);
logger.finest("File " + tmpFile + " type detected as " + fileType);
if (fileType == null) {
setErrorMessage("Could not determine the file type of file " + newName);
setStatus(TaskStatus.ERROR);
return;
}
// Run the import module on the decompressed file
RawDataFileWriter newMZmineFile = MZmineCore.createNewFile(newName);
decompressedOpeningTask = RawDataImportModule.createOpeningTask(fileType, project, tmpFile, newMZmineFile);
if (decompressedOpeningTask == null) {
setErrorMessage("File type " + fileType + " of file " + newName + " is not supported.");
setStatus(TaskStatus.ERROR);
return;
}
// Run the underlying task
decompressedOpeningTask.run();
// Delete the temporary folder
tmpFile.delete();
tmpDir.delete();
if (isCanceled())
return;
} catch (Throwable e) {
e.printStackTrace();
logger.log(Level.SEVERE, "Could not open file " + file.getPath(), e);
setErrorMessage(ExceptionUtils.exceptionToString(e));
setStatus(TaskStatus.ERROR);
return;
}
logger.info("Finished opening compressed file " + file);
// Update task status
setStatus(TaskStatus.FINISHED);
}
Aggregations