Search in sources :

Example 1 with StreamCopy

use of net.sf.mzmine.util.StreamCopy in project mzmine2 by mzmine.

the class ProjectSavingTask method saveConfiguration.

/**
 * Save the configuration file.
 *
 * @throws java.io.IOException
 */
private void saveConfiguration(ZipOutputStream zipStream) throws IOException {
    logger.info("Saving configuration file");
    currentSavedObjectName = "configuration";
    zipStream.putNextEntry(new ZipEntry(CONFIG_FILENAME));
    try {
        File tempConfigFile = File.createTempFile("mzmineconfig", ".tmp");
        MZmineCore.getConfiguration().saveConfiguration(tempConfigFile);
        FileInputStream fileStream = new FileInputStream(tempConfigFile);
        StreamCopy copyMachine = new StreamCopy();
        copyMachine.copy(fileStream, zipStream);
        fileStream.close();
        tempConfigFile.delete();
    } catch (Exception e) {
        e.printStackTrace();
        logger.warning("Could not save configuration" + ExceptionUtils.exceptionToString(e));
    }
}
Also used : StreamCopy(net.sf.mzmine.util.StreamCopy) ZipEntry(java.util.zip.ZipEntry) RawDataFile(net.sf.mzmine.datamodel.RawDataFile) File(java.io.File) FileInputStream(java.io.FileInputStream) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) IOException(java.io.IOException) SAXException(org.xml.sax.SAXException)

Example 2 with StreamCopy

use of net.sf.mzmine.util.StreamCopy in project mzmine2 by mzmine.

the class ProjectOpeningTask method loadConfiguration.

/**
 * Load the configuration file from the project zip file
 */
private void loadConfiguration(InputStream is) throws IOException {
    logger.info("Loading configuration file");
    currentLoadedObjectName = "Configuration";
    File tempConfigFile = File.createTempFile("mzmineconfig", ".tmp");
    FileOutputStream fileStream = new FileOutputStream(tempConfigFile);
    copyMachine = new StreamCopy();
    copyMachine.copy(is, fileStream);
    fileStream.close();
    try {
        MZmineCore.getConfiguration().loadConfiguration(tempConfigFile);
    } catch (Exception e) {
        logger.warning("Could not load configuration from the project: " + ExceptionUtils.exceptionToString(e));
    }
    tempConfigFile.delete();
}
Also used : StreamCopy(net.sf.mzmine.util.StreamCopy) FileOutputStream(java.io.FileOutputStream) RawDataFile(net.sf.mzmine.datamodel.RawDataFile) ZipFile(java.util.zip.ZipFile) File(java.io.File) IOException(java.io.IOException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) SAXException(org.xml.sax.SAXException)

Example 3 with StreamCopy

use of net.sf.mzmine.util.StreamCopy 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)

Example 4 with StreamCopy

use of net.sf.mzmine.util.StreamCopy in project mzmine2 by mzmine.

the class ProjectOpeningTask method loadScansFile.

private void loadScansFile(InputStream is, String fileID, String fileName) throws IOException {
    logger.info("Loading scans data #" + fileID + ": " + fileName);
    currentLoadedObjectName = fileName + " scan data";
    final File tempFile = RawDataFileImpl.createNewDataPointsFile();
    final FileOutputStream os = new FileOutputStream(tempFile);
    copyMachine = new StreamCopy();
    copyMachine.copy(is, os);
    os.close();
    scanFilesIDMap.put(fileID, tempFile);
}
Also used : StreamCopy(net.sf.mzmine.util.StreamCopy) FileOutputStream(java.io.FileOutputStream) RawDataFile(net.sf.mzmine.datamodel.RawDataFile) ZipFile(java.util.zip.ZipFile) File(java.io.File)

Aggregations

File (java.io.File)4 StreamCopy (net.sf.mzmine.util.StreamCopy)4 FileOutputStream (java.io.FileOutputStream)3 RawDataFile (net.sf.mzmine.datamodel.RawDataFile)3 FileInputStream (java.io.FileInputStream)2 IOException (java.io.IOException)2 ZipEntry (java.util.zip.ZipEntry)2 ZipFile (java.util.zip.ZipFile)2 SAXException (org.xml.sax.SAXException)2 InputStream (java.io.InputStream)1 GZIPInputStream (java.util.zip.GZIPInputStream)1 ZipInputStream (java.util.zip.ZipInputStream)1 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)1 TransformerConfigurationException (javax.xml.transform.TransformerConfigurationException)1 RawDataFileWriter (net.sf.mzmine.datamodel.RawDataFileWriter)1 RawDataFileType (net.sf.mzmine.modules.rawdatamethods.rawdataimport.RawDataFileType)1