use of java.util.zip.ZipOutputStream in project beam by apache.
the class TextIOTest method createZipFile.
/**
* Create a zip file with the given lines.
*
* @param expected A list of expected lines, populated in the zip file.
* @param filename Optionally zip file name (can be null).
* @param fieldsEntries Fields to write in zip entries.
* @return The zip filename.
* @throws Exception In case of a failure during zip file creation.
*/
private String createZipFile(List<String> expected, String filename, String[]... fieldsEntries) throws Exception {
File tmpFile = tempFolder.resolve(filename).toFile();
String tmpFileName = tmpFile.getPath();
ZipOutputStream out = new ZipOutputStream(new FileOutputStream(tmpFile));
PrintStream writer = new PrintStream(out, true);
int index = 0;
for (String[] entry : fieldsEntries) {
out.putNextEntry(new ZipEntry(Integer.toString(index)));
for (String field : entry) {
writer.println(field);
expected.add(field);
}
out.closeEntry();
index++;
}
writer.close();
out.close();
return tmpFileName;
}
use of java.util.zip.ZipOutputStream in project poi by apache.
the class SXSSFWorkbook method injectData.
protected void injectData(ZipEntrySource zipEntrySource, OutputStream out) throws IOException {
try {
ZipOutputStream zos = new ZipOutputStream(out);
try {
Enumeration<? extends ZipEntry> en = zipEntrySource.getEntries();
while (en.hasMoreElements()) {
ZipEntry ze = en.nextElement();
zos.putNextEntry(new ZipEntry(ze.getName()));
InputStream is = zipEntrySource.getInputStream(ze);
XSSFSheet xSheet = getSheetFromZipEntryName(ze.getName());
if (xSheet != null) {
SXSSFSheet sxSheet = getSXSSFSheet(xSheet);
InputStream xis = sxSheet.getWorksheetXMLInputStream();
try {
copyStreamAndInjectWorksheet(is, zos, xis);
} finally {
xis.close();
}
} else {
IOUtils.copy(is, zos);
}
is.close();
}
} finally {
zos.close();
}
} finally {
zipEntrySource.close();
}
}
use of java.util.zip.ZipOutputStream in project poi by apache.
the class AesZipFileZipEntrySource method copyToFile.
private static void copyToFile(InputStream is, File tmpFile, CipherAlgorithm cipherAlgorithm, byte[] keyBytes, byte[] ivBytes) throws IOException, GeneralSecurityException {
SecretKeySpec skeySpec = new SecretKeySpec(keyBytes, cipherAlgorithm.jceId);
Cipher ciEnc = CryptoFunctions.getCipher(skeySpec, cipherAlgorithm, ChainingMode.cbc, ivBytes, Cipher.ENCRYPT_MODE, "PKCS5Padding");
ZipInputStream zis = new ZipInputStream(is);
FileOutputStream fos = new FileOutputStream(tmpFile);
ZipOutputStream zos = new ZipOutputStream(fos);
ZipEntry ze;
while ((ze = zis.getNextEntry()) != null) {
// the cipher output stream pads the data, therefore we can't reuse the ZipEntry with set sizes
// as those will be validated upon close()
ZipEntry zeNew = new ZipEntry(ze.getName());
zeNew.setComment(ze.getComment());
zeNew.setExtra(ze.getExtra());
zeNew.setTime(ze.getTime());
// zeNew.setMethod(ze.getMethod());
zos.putNextEntry(zeNew);
FilterOutputStream fos2 = new FilterOutputStream(zos) {
// don't close underlying ZipOutputStream
@Override
public void close() {
}
};
CipherOutputStream cos = new CipherOutputStream(fos2, ciEnc);
IOUtils.copy(zis, cos);
cos.close();
fos2.close();
zos.closeEntry();
zis.closeEntry();
}
zos.close();
fos.close();
zis.close();
}
use of java.util.zip.ZipOutputStream in project poi by apache.
the class ZipPartMarshaller method marshall.
/**
* Save the specified part.
*
* @throws OpenXML4JException
* Throws if an internal exception is thrown.
*/
@Override
public boolean marshall(PackagePart part, OutputStream os) throws OpenXML4JException {
if (!(os instanceof ZipOutputStream)) {
logger.log(POILogger.ERROR, "Unexpected class " + os.getClass().getName());
throw new OpenXML4JException("ZipOutputStream expected !");
// Normally should happen only in developement phase, so just throw
// exception
}
// might depend on empty parts being saved, e.g. some unit tests verify this currently.
if (part.getSize() == 0 && part.getPartName().getName().equals(XSSFRelation.SHARED_STRINGS.getDefaultFileName())) {
return true;
}
ZipOutputStream zos = (ZipOutputStream) os;
ZipEntry partEntry = new ZipEntry(ZipHelper.getZipItemNameFromOPCName(part.getPartName().getURI().getPath()));
try {
// Create next zip entry
zos.putNextEntry(partEntry);
// Saving data in the ZIP file
InputStream ins = part.getInputStream();
byte[] buff = new byte[ZipHelper.READ_WRITE_FILE_BUFFER_SIZE];
while (ins.available() > 0) {
int resultRead = ins.read(buff);
if (resultRead == -1) {
// End of file reached
break;
}
zos.write(buff, 0, resultRead);
}
zos.closeEntry();
} catch (IOException ioe) {
logger.log(POILogger.ERROR, "Cannot write: " + part.getPartName() + ": in ZIP", ioe);
return false;
}
// Saving relationship part
if (part.hasRelationships()) {
PackagePartName relationshipPartName = PackagingURIHelper.getRelationshipPartName(part.getPartName());
marshallRelationshipPart(part.getRelationships(), relationshipPartName, zos);
}
return true;
}
use of java.util.zip.ZipOutputStream in project poi by apache.
the class OOXMLPrettyPrint method handleFile.
private static void handleFile(File file, File outFile) throws ZipException, IOException, TransformerException, ParserConfigurationException {
System.out.println("Reading zip-file " + file + " and writing pretty-printed XML to " + outFile);
ZipFile zipFile = ZipHelper.openZipFile(file);
try {
ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(outFile)));
try {
new OOXMLPrettyPrint().handle(zipFile, out);
} finally {
out.close();
}
} finally {
zipFile.close();
System.out.println();
}
}
Aggregations