private void preClose() throws IOException { if (finished) { throw new IOException("Stream has already been finished"); } if (entry == null) { throw new IOException("No current entry to close"); } if (!entry.hasWritten) { write(EMPTY, 0, 0); } }
zos.write( bytes );
private static void zipStoreBuffer(ZipArchiveOutputStream zip, String name, byte[] dataBuffer) throws IOException { ZipEntry zipEntry = new ZipEntry(name != null ? name : UUID.randomUUID().toString()); zipEntry.setMethod(ZipOutputStream.STORED); zipEntry.setSize(dataBuffer.length); CRC32 crc32 = new CRC32(); crc32.update(dataBuffer); zipEntry.setCrc(crc32.getValue()); try { zip.putArchiveEntry(new ZipArchiveEntry(zipEntry)); } catch (ZipException ex) { if (name != null) { zipStoreBuffer(zip, "x-" + name, dataBuffer); return; } } zip.write(dataBuffer); zip.closeArchiveEntry(); }
fis = new FileInputStream(f); while ((length = fis.read(buf, 0, buf.length)) >= 0) { zos.write(buf, 0, length);
private void preClose() throws IOException { if (finished) { throw new IOException("Stream has already been finished"); } if (entry == null) { throw new IOException("No current entry to close"); } if (!entry.hasWritten) { write(EMPTY, 0, 0); } }
@Override public void write(byte[] bytes) throws IOException { Preconditions.checkState(isOpen(), "ZipArchive is not open!"); outputZip.write(bytes); outputZip.flush(); }
/** * Recursively locates and adds files and folders to a zip archive * @param file * @param out * @param path * @throws IOException */ private static void pack(File file, ZipArchiveOutputStream out, String path) throws IOException { if(file.isDirectory()){ path = path + file.getName() +"/"; for(File afile: file.listFiles()){ pack(afile,out, path); } } else { ZipArchiveEntry entry = (ZipArchiveEntry) out.createArchiveEntry(file, path + file.getName()); out.putArchiveEntry(entry); byte[] buf = new byte[1024]; int len; FileInputStream in = new FileInputStream(file); while ((len = in.read(buf)) > 0) { out.write(buf, 0, len); } out.closeArchiveEntry(); } } }
byte[] zip(byte[] data, String filename) { ByteArrayOutputStream bos = new ByteArrayOutputStream(); ZipArchiveOutputStream zos = new ZipArchiveOutputStream(bos); ZipArchiveEntry entry = new ZipArchiveEntry(filename); entry.setSize(data.length); zos.putArchiveEntry(entry); zos.write(data); zos.closeArchiveEntry(); zos.close(); bos.close(); return bos.toByteArray(); }
protected byte[] zipContent(Map<String, Object> stringObjectMap) throws IOException { ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); ZipArchiveOutputStream zipOutputStream = new ZipArchiveOutputStream(byteArrayOutputStream); zipOutputStream.setMethod(ZipArchiveOutputStream.STORED); zipOutputStream.setEncoding(ENCODING); for (Map.Entry<String, Object> entry : stringObjectMap.entrySet()) { byte[] data = (byte[]) entry.getValue(); ArchiveEntry archiveEntry = newStoredEntry(entry.getKey(), data); zipOutputStream.putArchiveEntry(archiveEntry); zipOutputStream.write(data); zipOutputStream.closeArchiveEntry(); } zipOutputStream.close(); return byteArrayOutputStream.toByteArray(); }
@Override public byte[] exportEntitiesToZIP(Collection<? extends Entity> entities) { String json = entitySerialization.toJson(entities, null, EntitySerializationOption.COMPACT_REPEATED_ENTITIES); byte[] jsonBytes = json.getBytes(StandardCharsets.UTF_8); ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); ZipArchiveOutputStream zipOutputStream = new ZipArchiveOutputStream(byteArrayOutputStream); zipOutputStream.setMethod(ZipArchiveOutputStream.STORED); zipOutputStream.setEncoding(StandardCharsets.UTF_8.name()); ArchiveEntry singleDesignEntry = newStoredEntry("entities.json", jsonBytes); try { zipOutputStream.putArchiveEntry(singleDesignEntry); zipOutputStream.write(jsonBytes); zipOutputStream.closeArchiveEntry(); } catch (Exception e) { throw new RuntimeException("Error on creating zip archive during entities export", e); } finally { IOUtils.closeQuietly(zipOutputStream); } return byteArrayOutputStream.toByteArray(); }
@Override public byte[] exportFolder(Folder folder) throws IOException { ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); ZipArchiveOutputStream zipOutputStream = new ZipArchiveOutputStream(byteArrayOutputStream); zipOutputStream.setMethod(ZipArchiveOutputStream.STORED); zipOutputStream.setEncoding(StandardCharsets.UTF_8.name()); String xml = createXStream().toXML(folder); byte[] xmlBytes = xml.getBytes(StandardCharsets.UTF_8); ArchiveEntry zipEntryDesign = newStoredEntry("folder.xml", xmlBytes); zipOutputStream.putArchiveEntry(zipEntryDesign); zipOutputStream.write(xmlBytes); try { zipOutputStream.closeArchiveEntry(); } catch (Exception ex) { throw new RuntimeException(String.format("Exception occurred while exporting folder %s.", folder.getName())); } zipOutputStream.close(); return byteArrayOutputStream.toByteArray(); }
public static void writeArchivedLogTailToStream(File logFile, OutputStream outputStream) throws IOException { if (!logFile.exists()) { throw new FileNotFoundException(); } ZipArchiveOutputStream zipOutputStream = new ZipArchiveOutputStream(outputStream); zipOutputStream.setMethod(ZipArchiveOutputStream.DEFLATED); zipOutputStream.setEncoding(ZIP_ENCODING); byte[] content = getTailBytes(logFile); ArchiveEntry archiveEntry = newTailArchive(logFile.getName(), content); zipOutputStream.putArchiveEntry(archiveEntry); zipOutputStream.write(content); zipOutputStream.closeArchiveEntry(); zipOutputStream.close(); }
zipEntry.setUnixMode(UnixStat.LINK_FLAG | UnixStat.DEFAULT_FILE_PERM); tOut.putArchiveEntry(zipEntry); tOut.write(Files.readSymbolicLink(f).toString().getBytes()); tOut.closeArchiveEntry(); } else {
zipEntry.setUnixMode(UnixStat.LINK_FLAG | UnixStat.DEFAULT_FILE_PERM); tOut.putArchiveEntry(zipEntry); tOut.write(Files.readSymbolicLink(f).toString().getBytes()); tOut.closeArchiveEntry(); } else {
import org.apache.commons.compress.archivers.ArchiveEntry; import org.apache.commons.compress.archivers.zip.ZipArchiveEntry; import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream; import java.util.Arrays; public class Test { public static void main(String[] args) throws Exception { ZipArchiveOutputStream zipOut = new ZipArchiveOutputStream(System.out); byte[] data = new byte[1024*1024]; for(byte i=49; i<120; i++) { Arrays.fill(data, i); ArchiveEntry file1 = new ZipArchiveEntry("file" + i + ".txt"); zipOut.putArchiveEntry(file1); zipOut.write(data); zipOut.closeArchiveEntry(); } zipOut.finish(); zipOut.close(); } }
zipOutputStream.write(reportDocument.getContent());
@Override public byte[] exportReports(Collection<Report> reports) { ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); ZipArchiveOutputStream zipOutputStream = new ZipArchiveOutputStream(byteArrayOutputStream); try { zipOutputStream.setMethod(ZipArchiveOutputStream.STORED); zipOutputStream.setEncoding(ENCODING); for (Report report : reports) { try { byte[] reportBytes = exportReport(report); ArchiveEntry singleReportEntry = newStoredEntry(replaceForbiddenCharacters(report.getName()) + ".zip", reportBytes); zipOutputStream.putArchiveEntry(singleReportEntry); zipOutputStream.write(reportBytes); zipOutputStream.closeArchiveEntry(); } catch (IOException e) { throw new ReportingException(String.format("Exception occurred while exporting report [%s]", report.getName()), e); } } } finally { IOUtils.closeQuietly(zipOutputStream); } return byteArrayOutputStream.toByteArray(); }
ArchiveEntry zipEntryReportObject = newStoredEntry("report.structure", xmlBytes); zipOutputStream.putArchiveEntry(zipEntryReportObject); zipOutputStream.write(xmlBytes); "templates/" + i + "/" + template.getName(), fileBytes); zipOutputStream.putArchiveEntry(zipEntryTemplate); zipOutputStream.write(fileBytes);
zos.write( bytes );