Search in sources :

Example 1 with RawDataFileType

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);
}
Also used : RawDataFileType(net.sf.mzmine.modules.rawdatamethods.rawdataimport.RawDataFileType) GZIPInputStream(java.util.zip.GZIPInputStream) ZipInputStream(java.util.zip.ZipInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) StreamCopy(net.sf.mzmine.util.StreamCopy) ZipEntry(java.util.zip.ZipEntry) RawDataFileWriter(net.sf.mzmine.datamodel.RawDataFileWriter) FileInputStream(java.io.FileInputStream) GZIPInputStream(java.util.zip.GZIPInputStream) ZipInputStream(java.util.zip.ZipInputStream) FileOutputStream(java.io.FileOutputStream) File(java.io.File)

Aggregations

File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 FileOutputStream (java.io.FileOutputStream)1 InputStream (java.io.InputStream)1 GZIPInputStream (java.util.zip.GZIPInputStream)1 ZipEntry (java.util.zip.ZipEntry)1 ZipInputStream (java.util.zip.ZipInputStream)1 RawDataFileWriter (net.sf.mzmine.datamodel.RawDataFileWriter)1 RawDataFileType (net.sf.mzmine.modules.rawdatamethods.rawdataimport.RawDataFileType)1 StreamCopy (net.sf.mzmine.util.StreamCopy)1