Search in sources :

Example 86 with ZipInputStream

use of java.util.zip.ZipInputStream in project nifi by apache.

the class TestMergeContent method testZip.

@Test
public void testZip() throws IOException {
    final TestRunner runner = TestRunners.newTestRunner(new MergeContent());
    runner.setProperty(MergeContent.MAX_BIN_AGE, "1 sec");
    runner.setProperty(MergeContent.MERGE_FORMAT, MergeContent.MERGE_FORMAT_ZIP);
    createFlowFiles(runner);
    runner.run();
    runner.assertQueueEmpty();
    runner.assertTransferCount(MergeContent.REL_MERGED, 1);
    runner.assertTransferCount(MergeContent.REL_FAILURE, 0);
    runner.assertTransferCount(MergeContent.REL_ORIGINAL, 3);
    final MockFlowFile bundle = runner.getFlowFilesForRelationship(MergeContent.REL_MERGED).get(0);
    try (final InputStream rawIn = new ByteArrayInputStream(runner.getContentAsByteArray(bundle));
        final ZipInputStream in = new ZipInputStream(rawIn)) {
        Assert.assertNotNull(in.getNextEntry());
        final byte[] part1 = IOUtils.toByteArray(in);
        Assert.assertTrue(Arrays.equals("Hello".getBytes("UTF-8"), part1));
        in.getNextEntry();
        final byte[] part2 = IOUtils.toByteArray(in);
        Assert.assertTrue(Arrays.equals(", ".getBytes("UTF-8"), part2));
        in.getNextEntry();
        final byte[] part3 = IOUtils.toByteArray(in);
        Assert.assertTrue(Arrays.equals("World!".getBytes("UTF-8"), part3));
    }
    bundle.assertAttributeEquals(CoreAttributes.MIME_TYPE.key(), "application/zip");
}
Also used : MockFlowFile(org.apache.nifi.util.MockFlowFile) ZipInputStream(java.util.zip.ZipInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) TestRunner(org.apache.nifi.util.TestRunner) ZipInputStream(java.util.zip.ZipInputStream) TarArchiveInputStream(org.apache.commons.compress.archivers.tar.TarArchiveInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Test(org.junit.Test)

Example 87 with ZipInputStream

use of java.util.zip.ZipInputStream in project nifi by apache.

the class ZipUnpackerSequenceFileWriter method processInputStream.

@Override
protected void processInputStream(InputStream stream, final FlowFile flowFile, final Writer writer) throws IOException {
    try (final ZipInputStream zipIn = new ZipInputStream(new BufferedInputStream(stream))) {
        ZipEntry zipEntry;
        while ((zipEntry = zipIn.getNextEntry()) != null) {
            if (zipEntry.isDirectory()) {
                continue;
            }
            final File file = new File(zipEntry.getName());
            final String key = file.getName();
            long fileSize = zipEntry.getSize();
            final InputStreamWritable inStreamWritable = new InputStreamWritable(zipIn, (int) fileSize);
            writer.append(new Text(key), inStreamWritable);
            logger.debug("Appending FlowFile {} to Sequence File", new Object[] { key });
        }
    }
}
Also used : InputStreamWritable(org.apache.nifi.processors.hadoop.util.InputStreamWritable) ZipInputStream(java.util.zip.ZipInputStream) BufferedInputStream(org.apache.nifi.stream.io.BufferedInputStream) ZipEntry(java.util.zip.ZipEntry) Text(org.apache.hadoop.io.Text) FlowFile(org.apache.nifi.flowfile.FlowFile) File(java.io.File)

Example 88 with ZipInputStream

use of java.util.zip.ZipInputStream in project ice by JBEI.

the class TraceSequences method addTraceSequence.

public boolean addTraceSequence(File file, String uploadFileName) {
    entryAuthorization.expectRead(userId, entry);
    FileInputStream inputStream;
    try {
        inputStream = new FileInputStream(file);
    } catch (FileNotFoundException e) {
        Logger.error(e);
        return false;
    }
    if (uploadFileName.toLowerCase().endsWith(".zip")) {
        try (ZipInputStream zis = new ZipInputStream(inputStream)) {
            ZipEntry zipEntry;
            while (true) {
                zipEntry = zis.getNextEntry();
                if (zipEntry != null) {
                    if (!zipEntry.isDirectory() && !zipEntry.getName().startsWith("__MACOSX")) {
                        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
                        int c;
                        while ((c = zis.read()) != -1) {
                            byteArrayOutputStream.write(c);
                        }
                        String zipFilename = Paths.get(zipEntry.getName()).getFileName().toString();
                        boolean parsed = parseTraceSequence(zipFilename, byteArrayOutputStream.toByteArray());
                        if (!parsed) {
                            String errMsg = ("Could not parse \"" + zipEntry.getName() + "\". Only Fasta, GenBank & ABI files are supported.");
                            Logger.error(errMsg);
                            return false;
                        }
                    }
                } else {
                    break;
                }
            }
        } catch (IOException e) {
            String errMsg = ("Could not parse zip file.");
            Logger.error(errMsg);
            return false;
        }
    } else {
        try {
            boolean parsed = parseTraceSequence(uploadFileName, IOUtils.toByteArray(inputStream));
            if (!parsed) {
                String errMsg = ("Could not parse \"" + uploadFileName + "\". Only Fasta, GenBank & ABI files are supported.");
                Logger.error(errMsg);
                return false;
            }
        } catch (IOException e) {
            Logger.error(e);
            return false;
        }
    }
    return true;
}
Also used : ZipInputStream(java.util.zip.ZipInputStream) ZipEntry(java.util.zip.ZipEntry)

Example 89 with ZipInputStream

use of java.util.zip.ZipInputStream in project processdash by dtuma.

the class FileBackupManager method createExternalizedBackupFile.

private File createExternalizedBackupFile(URL backup) throws IOException {
    // open the existing backup file as a ZIP stream
    ZipInputStream zipIn = new ZipInputStream(new BufferedInputStream(backup.openStream()));
    // create a temporary file for externalizing purposes
    File result = TempFileFactory.get().createTempFile("pdash-backup", ".zip");
    result.deleteOnExit();
    ZipOutputStream zipOut = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(result)));
    zipOut.setLevel(9);
    ExternalResourceManager extMgr = ExternalResourceManager.getInstance();
    // copy all the files from the existing backup into the externalized
    // backup (but skip any files that appear to be externalized)
    ZipEntry e;
    while ((e = zipIn.getNextEntry()) != null) {
        String filename = e.getName();
        if (extMgr.isArchivedItem(filename))
            continue;
        ZipEntry eOut = new ZipEntry(filename);
        eOut.setTime(e.getTime());
        zipOut.putNextEntry(eOut);
        FileUtils.copyFile(zipIn, zipOut);
        zipOut.closeEntry();
    }
    zipIn.close();
    // now, ask the external resource manager to augment the ZIP.
    extMgr.addExternalResourcesToBackup(zipOut);
    zipOut.finish();
    zipOut.close();
    return result;
}
Also used : ZipInputStream(java.util.zip.ZipInputStream) ExternalResourceManager(net.sourceforge.processdash.tool.export.mgr.ExternalResourceManager) BufferedInputStream(java.io.BufferedInputStream) ZipOutputStream(java.util.zip.ZipOutputStream) FileOutputStream(java.io.FileOutputStream) ZipEntry(java.util.zip.ZipEntry) File(java.io.File) BufferedOutputStream(java.io.BufferedOutputStream)

Example 90 with ZipInputStream

use of java.util.zip.ZipInputStream in project processdash by dtuma.

the class WBSSynchronizer method getUserDumpData.

private Element getUserDumpData(File f) {
    FileInputStream fileInputStream = null;
    Element result = null;
    String datasetID = null;
    try {
        fileInputStream = new FileInputStream(f);
        ZipInputStream zipIn = new ZipInputStream(new BufferedInputStream(fileInputStream));
        NonclosingInputStream nis = new NonclosingInputStream(zipIn);
        ZipEntry e;
        while ((e = zipIn.getNextEntry()) != null) {
            if (e.getName().equals(USER_DUMP_ENTRY_NAME)) {
                result = XMLUtils.parse(nis).getDocumentElement();
                if (result.hasAttribute(DATASET_ID_ATTR))
                    break;
            } else if (e.getName().equals(MANIFEST_ENTRY_NAME)) {
                datasetID = getDatasetIdFromManifest(nis);
            }
        }
    } catch (Exception e) {
        logger.severe("Unable to read user dump data from file " + f);
        e.printStackTrace();
    }
    FileUtils.safelyClose(fileInputStream);
    if (result == null) {
        // the user's exported pdash file did not contain a dump file.
        logger.fine("No " + USER_DUMP_ENTRY_NAME + " file found in " + f);
    } else if (XMLUtils.hasValue(datasetID)) {
        result.setAttribute(DATASET_ID_ATTR, datasetID);
    }
    return result;
}
Also used : ZipInputStream(java.util.zip.ZipInputStream) BufferedInputStream(java.io.BufferedInputStream) Element(org.w3c.dom.Element) ZipEntry(java.util.zip.ZipEntry) NonclosingInputStream(net.sourceforge.processdash.util.NonclosingInputStream) FileInputStream(java.io.FileInputStream) MalformedURLException(java.net.MalformedURLException) SAXException(org.xml.sax.SAXException)

Aggregations

ZipInputStream (java.util.zip.ZipInputStream)968 ZipEntry (java.util.zip.ZipEntry)762 IOException (java.io.IOException)355 File (java.io.File)319 FileInputStream (java.io.FileInputStream)316 InputStream (java.io.InputStream)203 FileOutputStream (java.io.FileOutputStream)198 ByteArrayInputStream (java.io.ByteArrayInputStream)190 ByteArrayOutputStream (java.io.ByteArrayOutputStream)138 BufferedInputStream (java.io.BufferedInputStream)127 ZipOutputStream (java.util.zip.ZipOutputStream)91 Test (org.junit.Test)89 ArrayList (java.util.ArrayList)80 OutputStream (java.io.OutputStream)67 URL (java.net.URL)58 Path (java.nio.file.Path)58 FileNotFoundException (java.io.FileNotFoundException)56 HashMap (java.util.HashMap)56 BufferedOutputStream (java.io.BufferedOutputStream)54 ZipFile (java.util.zip.ZipFile)43