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");
}
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 });
}
}
}
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;
}
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;
}
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;
}
Aggregations