本文整理汇总了Java中org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream.write方法的典型用法代码示例。如果您正苦于以下问题:Java ZipArchiveOutputStream.write方法的具体用法?Java ZipArchiveOutputStream.write怎么用?Java ZipArchiveOutputStream.write使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream
的用法示例。
在下文中一共展示了ZipArchiveOutputStream.write方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: writeArchivedLogTailToStream
import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream; //导入方法依赖的package包/类
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();
}
示例2: exportFolder
import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream; //导入方法依赖的package包/类
@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();
}
示例3: exportEntitiesToZIP
import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream; //导入方法依赖的package包/类
@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();
}
示例4: makeSourceZipFile
import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream; //导入方法依赖的package包/类
/**
* Creates a zip file with random content.
*
* @author S3460
* @param source the source
* @return the zip file
* @throws Exception the exception
*/
private ZipFile makeSourceZipFile(File source) throws Exception {
ZipArchiveOutputStream out = new ZipArchiveOutputStream(new FileOutputStream(source));
int size = randomSize(entryMaxSize);
for (int i = 0; i < size; i++) {
out.putArchiveEntry(new ZipArchiveEntry("zipentry" + i));
int anz = randomSize(10);
for (int j = 0; j < anz; j++) {
byte[] bytes = getRandomBytes();
out.write(bytes, 0, bytes.length);
}
out.flush();
out.closeArchiveEntry();
}
//add leeres Entry
out.putArchiveEntry(new ZipArchiveEntry("zipentry" + size));
out.flush();
out.closeArchiveEntry();
out.flush();
out.finish();
out.close();
return new ZipFile(source);
}
示例5: makeTargetZipFile
import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream; //导入方法依赖的package包/类
/**
* Writes a modified version of zip_Source into target.
*
* @author S3460
* @param zipSource the zip source
* @param target the target
* @return the zip file
* @throws Exception the exception
*/
private ZipFile makeTargetZipFile(ZipFile zipSource, File target) throws Exception {
ZipArchiveOutputStream out = new ZipArchiveOutputStream(new FileOutputStream(target));
for (Enumeration<ZipArchiveEntry> enumer = zipSource.getEntries(); enumer.hasMoreElements();) {
ZipArchiveEntry sourceEntry = enumer.nextElement();
out.putArchiveEntry(new ZipArchiveEntry(sourceEntry.getName()));
byte[] oldBytes = toBytes(zipSource, sourceEntry);
byte[] newBytes = getRandomBytes();
byte[] mixedBytes = mixBytes(oldBytes, newBytes);
out.write(mixedBytes, 0, mixedBytes.length);
out.flush();
out.closeArchiveEntry();
}
out.putArchiveEntry(new ZipArchiveEntry("zipentry" + entryMaxSize + 1));
byte[] bytes = getRandomBytes();
out.write(bytes, 0, bytes.length);
out.flush();
out.closeArchiveEntry();
out.putArchiveEntry(new ZipArchiveEntry("zipentry" + (entryMaxSize + 2)));
out.closeArchiveEntry();
out.flush();
out.finish();
out.close();
return new ZipFile(targetFile);
}
示例6: zip
import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream; //导入方法依赖的package包/类
/**
* Add one file to the open zip archive stream. Drops the file if it is a directory
*
* @param fileToZip
* @param baseDir
* @param os
* @throws Throwable
*/
public static void zip(File fileToZip, ZipArchiveOutputStream os) throws Throwable {
if (fileToZip.isDirectory()) return;
byte buffer[] = new byte[BUFFER_SIZE];
String name = fileToZip.getName();
ZipArchiveEntry entry = new ZipArchiveEntry(fileToZip, name);
entry.setSize(fileToZip.length());
os.putArchiveEntry(entry);
BufferedInputStream is = new BufferedInputStream(new FileInputStream(fileToZip), BUFFER_SIZE);
int count;
try {
while ((count = is.read(buffer, 0, BUFFER_SIZE)) != -1) {
os.write(buffer, 0, count);
}
} finally {
is.close();
}
os.closeArchiveEntry();
}
示例7: sendMetadataCompressed
import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream; //导入方法依赖的package包/类
@Override
protected void sendMetadataCompressed(String fileName, byte[] content, long lastModified,
ZipArchiveOutputStream container) throws IOException {
ZipArchiveEntry entry = new ZipArchiveEntry(fileName);
entry.setSize(content.length);
entry.setTime(lastModified);
container.putArchiveEntry(entry);
container.write(content);
container.closeArchiveEntry();
}
示例8: zipRepository
import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream; //导入方法依赖的package包/类
/**
* <p>
* Recursively browses the given {@code root} repository elements to populate the given {@code zipOutputStream} with
* corresponding files.
* </p>
* <p>
* If a referenced file cannot be found in the storage folder, it will be ignored (a {@code WARN} log is generated).
* </p>
*
* @param root
* The root repository element.
* @param zipOutputStream
* The stream to populate with files hierarchy.
* @param actualPath
* The current repository path.
*/
private void zipRepository(final RepositoryElement root, final ZipArchiveOutputStream zipOutputStream, final String actualPath) {
final String path = (actualPath.equals("") ? root.getName() : actualPath + "/" + root.getName());
if (root instanceof FileElement) {
final FileElement file = (FileElement) root;
final String fileStorageId = file.getStorageId();
if(fileStorageProvider.exists(fileStorageId)) {
try (final InputStream is = new BufferedInputStream(fileStorageProvider.open(fileStorageId), ResponseHelper.BUFFER_SIZE)) {
zipOutputStream.putArchiveEntry(new ZipArchiveEntry(path));
final byte data[] = new byte[ResponseHelper.BUFFER_SIZE];
while ((is.read(data)) != -1) {
zipOutputStream.write(data);
}
zipOutputStream.closeArchiveEntry();
} catch (final IOException e) {
LOG.warn("File '" + fileStorageId + "' cannot be found ; continuing with next file.", e);
}
} else {
LOG.warn("File '{0}' does not exists on the server ; continuing with next file.", fileStorageId);
}
} else if (root instanceof FolderElement) {
final FolderElement folder = (FolderElement) root;
for (final RepositoryElement element : folder.getChildren()) {
zipRepository(element, zipOutputStream, path);
}
}
}
示例9: writeToZip
import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream; //导入方法依赖的package包/类
public void writeToZip(ZipArchiveOutputStream zipOutput) throws IOException {
if (contentInputStream == null)
return;
String[] domainParts = StringUtils.split(uri.getHost(), '.');
StringBuilder path = new StringBuilder();
for (int i = domainParts.length - 1; i >= 0; i--) {
path.append(domainParts[i]);
path.append('/');
}
String[] pathParts = StringUtils.split(uri.getPath(), '/');
for (int i = 0; i < pathParts.length - 1; i++) {
if (StringUtils.isEmpty(pathParts[i]))
continue;
path.append(pathParts[i]);
path.append('/');
}
if (contentDispositionFilename != null)
path.append(contentDispositionFilename);
else {
String lastPart = pathParts == null || pathParts.length == 0 ? null : pathParts[pathParts.length - 1];
if (StringUtils.isEmpty(lastPart))
path.append("index");
else
path.append(lastPart);
}
if (uri.getPath().endsWith("/"))
path.append("/_index");
String query = uri.getQuery();
String fragment = uri.getFragment();
if (!StringUtils.isEmpty(query) || !StringUtils.isEmpty(fragment)) {
CRC32 crc32 = new CRC32();
if (!StringUtils.isEmpty(query))
crc32.update(query.getBytes());
if (!StringUtils.isEmpty(fragment))
crc32.update(fragment.getBytes());
path.append('.');
path.append(crc32.getValue());
}
ZipArchiveEntry zipEntry = new ZipArchiveEntry(path.toString());
zipOutput.putArchiveEntry(zipEntry);
BufferedInputStream bis = null;
byte[] buffer = new byte[65536];
try {
bis = new BufferedInputStream(contentInputStream);
int l;
while ((l = bis.read(buffer)) != -1)
zipOutput.write(buffer, 0, l);
zipOutput.closeArchiveEntry();
} finally {
IOUtils.close(bis);
}
}
示例10: addToZip
import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream; //导入方法依赖的package包/类
public void addToZip(NodeRef node, ZipArchiveOutputStream out, boolean noaccent, String path) throws IOException {
QName nodeQnameType = this.nodeService.getType(node);
// Special case : links
if (this.dictionaryService.isSubClass(nodeQnameType, ApplicationModel.TYPE_FILELINK)) {
NodeRef linkDestinationNode = (NodeRef) nodeService.getProperty(node, ContentModel.PROP_LINK_DESTINATION);
if (linkDestinationNode == null) {
return;
}
// Duplicate entry: check if link is not in the same space of the link destination
if (nodeService.getPrimaryParent(node).getParentRef().equals(nodeService.getPrimaryParent(linkDestinationNode).getParentRef())) {
return;
}
nodeQnameType = this.nodeService.getType(linkDestinationNode);
node = linkDestinationNode;
}
String nodeName = (String) nodeService.getProperty(node, ContentModel.PROP_NAME);
nodeName = noaccent ? unAccent(nodeName) : nodeName;
if (this.dictionaryService.isSubClass(nodeQnameType, ContentModel.TYPE_CONTENT)) {
ContentReader reader = contentService.getReader(node, ContentModel.PROP_CONTENT);
if (reader != null) {
InputStream is = reader.getContentInputStream();
String filename = path.isEmpty() ? nodeName : path + '/' + nodeName;
ZipArchiveEntry entry = new ZipArchiveEntry(filename);
entry.setTime(((Date) nodeService.getProperty(node, ContentModel.PROP_MODIFIED)).getTime());
entry.setSize(reader.getSize());
out.putArchiveEntry(entry);
byte buffer[] = new byte[BUFFER_SIZE];
while (true) {
int nRead = is.read(buffer, 0, buffer.length);
if (nRead <= 0) {
break;
}
out.write(buffer, 0, nRead);
}
is.close();
out.closeArchiveEntry();
}
else {
logger.warn("Could not read : " + nodeName + "content");
}
}
else if(this.dictionaryService.isSubClass(nodeQnameType, ContentModel.TYPE_FOLDER)
&& !this.dictionaryService.isSubClass(nodeQnameType, ContentModel.TYPE_SYSTEM_FOLDER)) {
List<ChildAssociationRef> children = nodeService
.getChildAssocs(node);
if (children.isEmpty()) {
String folderPath = path.isEmpty() ? nodeName + '/' : path + '/' + nodeName + '/';
out.putArchiveEntry(new ZipArchiveEntry(new ZipEntry(folderPath)));
} else {
for (ChildAssociationRef childAssoc : children) {
NodeRef childNodeRef = childAssoc.getChildRef();
addToZip(childNodeRef, out, noaccent,
path.isEmpty() ? nodeName : path + '/' + nodeName);
}
}
} else {
logger.info("Unmanaged type: "
+ nodeQnameType.getPrefixedQName(this.namespaceService)
+ ", filename: " + nodeName);
}
}
示例11: computeDelta
import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream; //导入方法依赖的package包/类
/**
* Computes the binary differences of two zip files. For all files contained in source and target which
* are not equal, the binary difference is caluclated by using
* {@link com.nothome.delta.Delta#compute(byte[], InputStream, DiffWriter)}.
* If the files are equal, nothing is written to the output for them.
* Files contained only in target and files to small for {@link com.nothome.delta.Delta} are copied to output.
* Files contained only in source are ignored.
* At last a list of all files contained in target is written to <code>META-INF/file.list</code> in output.
*
* @param sourceName the original zip file
* @param targetName a modification of the original zip file
* @param source the original zip file
* @param target a modification of the original zip file
* @param output the zip file where the patches have to be written to
* @throws IOException if an error occurs reading or writing any entry in a zip file
*/
public void computeDelta(String sourceName, String targetName, ZipFile source, ZipFile target, ZipArchiveOutputStream output) throws IOException {
ByteArrayOutputStream listBytes = new ByteArrayOutputStream();
PrintWriter list = new PrintWriter(new OutputStreamWriter(listBytes));
list.println(sourceName);
list.println(targetName);
computeDelta(source, target, output, list, "");
list.close();
ZipArchiveEntry listEntry = new ZipArchiveEntry("META-INF/file.list");
output.putArchiveEntry(listEntry);
output.write(listBytes.toByteArray());
output.closeArchiveEntry();
output.finish();
output.flush();
}