本文整理匯總了Java中org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream.putArchiveEntry方法的典型用法代碼示例。如果您正苦於以下問題:Java ZipArchiveOutputStream.putArchiveEntry方法的具體用法?Java ZipArchiveOutputStream.putArchiveEntry怎麽用?Java ZipArchiveOutputStream.putArchiveEntry使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream
的用法示例。
在下文中一共展示了ZipArchiveOutputStream.putArchiveEntry方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的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: zip
import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream; //導入方法依賴的package包/類
private File zip(File destination, String name) {
try {
File z = new File(Files.createTempDir(), name+".zip");
ZipArchiveOutputStream out = ZipHelper.setupZipOutputStream(new FileOutputStream(z));
for(String f : files(destination, "")) {
out.putArchiveEntry(new ZipArchiveEntry(f));
FileInputStream in = new FileInputStream(new File(destination, f));
IOUtils.copy(in, out);
in.close();
out.closeArchiveEntry();
}
out.close();
return z;
} catch(Exception e) {
throw new RuntimeException(e);
}
}
示例8: addToZipFile
import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream; //導入方法依賴的package包/類
public static File addToZipFile(final File directory, final String zipFilename, final String entryFilename, final String entryContent) throws IOException {
final File zipFile = File.createTempFile(zipFilename, ".zip", directory);
final FileOutputStream fileOutputStream = new FileOutputStream(zipFile);
try {
final ZipArchiveOutputStream zipArchiveOutputStream = new ZipArchiveOutputStream(fileOutputStream);
zipArchiveOutputStream.putArchiveEntry(new ZipArchiveEntry(entryFilename));
IOUtils.write(entryContent.getBytes(), zipArchiveOutputStream);
zipArchiveOutputStream.closeArchiveEntry();
zipArchiveOutputStream.finish();
} finally {
IOUtils.closeQuietly(fileOutputStream);
}
return zipFile;
}
示例9: addFileToZip
import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream; //導入方法依賴的package包/類
/**
* Creates a zip entry for the path specified with a name built from the base
* passed in and the file/directory name. If the path is a directory, a
* recursive call is made such that the full directory is added to the zip.
*
* @param z_out output stream to a Zip file
* @param path of the file/directory to add
* @param base prefix to the name of the zip file entry
*
* @throws IOException if anything goes wrong
*/
private static void addFileToZip(ZipArchiveOutputStream z_out, String path, String base)
throws IOException
{
File f = new File(path);
String entryName = base + f.getName();
ZipArchiveEntry zipEntry = new ZipArchiveEntry(f, entryName);
z_out.putArchiveEntry(zipEntry);
if (f.isFile())
{
try (FileInputStream fInputStream = new FileInputStream(f))
{
org.apache.commons.compress.utils.IOUtils.copy(fInputStream, z_out, 65535);
z_out.closeArchiveEntry();
}
}
else
{
z_out.closeArchiveEntry();
File[] children = f.listFiles();
if (children != null)
{
for (File child: children)
{
LOGGER.debug("ZIP Adding {}", child.getName());
addFileToZip(z_out, child.getAbsolutePath(), entryName + "/");
}
}
}
}
示例10: sendCompressedDirectory
import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream; //導入方法依賴的package包/類
@Override
protected void sendCompressedDirectory(MCRPath file, BasicFileAttributes attrs, ZipArchiveOutputStream container)
throws IOException {
ZipArchiveEntry entry = new ZipArchiveEntry(getFilename(file) + "/");
entry.setTime(attrs.lastModifiedTime().toMillis());
container.putArchiveEntry(entry);
container.closeArchiveEntry();
}
示例11: sendCompressedFile
import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream; //導入方法依賴的package包/類
@Override
protected void sendCompressedFile(MCRPath file, BasicFileAttributes attrs, ZipArchiveOutputStream container)
throws IOException {
ZipArchiveEntry entry = new ZipArchiveEntry(getFilename(file));
entry.setTime(attrs.lastModifiedTime().toMillis());
entry.setSize(attrs.size());
container.putArchiveEntry(entry);
try {
Files.copy(file, container);
} finally {
container.closeArchiveEntry();
}
}
示例12: 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();
}
示例13: makeZip
import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream; //導入方法依賴的package包/類
public static void makeZip() throws IOException, ArchiveException{
File f1 = new File("D:/compresstest.txt");
File f2 = new File("D:/test1.xml");
ByteArrayOutputStream baos = new ByteArrayOutputStream();
//ArchiveOutputStream ostemp = new ArchiveStreamFactory().createArchiveOutputStream("zip", baos);
ZipArchiveOutputStream ostemp = new ZipArchiveOutputStream(baos);
ostemp.setEncoding("GBK");
ostemp.putArchiveEntry(new ZipArchiveEntry(f1.getName()));
IOUtils.copy(new FileInputStream(f1), ostemp);
ostemp.closeArchiveEntry();
ostemp.putArchiveEntry(new ZipArchiveEntry(f2.getName()));
IOUtils.copy(new FileInputStream(f2), ostemp);
ostemp.closeArchiveEntry();
ostemp.finish();
ostemp.close();
// final OutputStream out = new FileOutputStream("D:/testcompress.zip");
final OutputStream out = new FileOutputStream("D:/中文名字.zip");
ArchiveOutputStream os = new ArchiveStreamFactory().createArchiveOutputStream("zip", out);
os.putArchiveEntry(new ZipArchiveEntry("打包.zip"));
baos.writeTo(os);
os.closeArchiveEntry();
baos.close();
os.finish();
os.close();
}
示例14: createZip
import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream; //導入方法依賴的package包/類
/**
* Create ZIP archive from file
*/
public static void createZip(File path, OutputStream os) throws IOException {
log.debug("createZip({}, {})", new Object[]{path, os});
if (path.exists() && path.canRead()) {
ZipArchiveOutputStream zaos = new ZipArchiveOutputStream(os);
zaos.setComment("Generated by OpenKM");
zaos.setCreateUnicodeExtraFields(UnicodeExtraFieldPolicy.ALWAYS);
zaos.setUseLanguageEncodingFlag(true);
zaos.setFallbackToUTF8(true);
zaos.setEncoding("UTF-8");
log.debug("FILE {}", path);
ZipArchiveEntry zae = new ZipArchiveEntry(path.getName());
zaos.putArchiveEntry(zae);
FileInputStream fis = new FileInputStream(path);
IOUtils.copy(fis, zaos);
fis.close();
zaos.closeArchiveEntry();
zaos.flush();
zaos.finish();
zaos.close();
} else {
throw new IOException("Can't access " + path);
}
log.debug("createZip: void");
}
示例15: writeArchivedLogToStream
import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream; //導入方法依賴的package包/類
public static void writeArchivedLogToStream(File logFile, OutputStream outputStream) throws IOException {
if (!logFile.exists()) {
throw new FileNotFoundException();
}
File tempFile = File.createTempFile(FilenameUtils.getBaseName(logFile.getName()) + "_log_", ".zip");
ZipArchiveOutputStream zipOutputStream = new ZipArchiveOutputStream(tempFile);
zipOutputStream.setMethod(ZipArchiveOutputStream.DEFLATED);
zipOutputStream.setEncoding(ZIP_ENCODING);
ArchiveEntry archiveEntry = newArchive(logFile);
zipOutputStream.putArchiveEntry(archiveEntry);
FileInputStream logFileInput = new FileInputStream(logFile);
IOUtils.copyLarge(logFileInput, zipOutputStream);
logFileInput.close();
zipOutputStream.closeArchiveEntry();
zipOutputStream.close();
FileInputStream tempFileInput = new FileInputStream(tempFile);
IOUtils.copyLarge(tempFileInput, outputStream);
tempFileInput.close();
FileUtils.deleteQuietly(tempFile);
}