本文整理汇总了Java中org.apache.tools.zip.ZipOutputStream.setMethod方法的典型用法代码示例。如果您正苦于以下问题:Java ZipOutputStream.setMethod方法的具体用法?Java ZipOutputStream.setMethod怎么用?Java ZipOutputStream.setMethod使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.apache.tools.zip.ZipOutputStream
的用法示例。
在下文中一共展示了ZipOutputStream.setMethod方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: downloadFiles
import org.apache.tools.zip.ZipOutputStream; //导入方法依赖的package包/类
/**
* Downloads a collection of files within a directory.
*
* @param response The HTTP response.
* @param status The download status.
* @param dir The directory.
* @param indexes Only download files with these indexes within the directory.
* @throws IOException If an I/O error occurs.
*/
private void downloadFiles(HttpServletResponse response, TransferStatus status, File dir, int[] indexes) throws IOException {
String zipFileName = dir.getName() + ".zip";
LOG.info("Starting to download '" + zipFileName + "' to " + status.getPlayer());
status.setFile(dir);
response.setContentType("application/x-download");
response.setHeader("Content-Disposition", "attachment; filename*=UTF-8''"+ encodeAsRFC5987(zipFileName));
ZipOutputStream out = new ZipOutputStream(response.getOutputStream());
out.setMethod(ZipOutputStream.STORED); // No compression.
MediaFile parent = mediaFileService.getMediaFile(dir);
List<MediaFile> allChildren = mediaFileService.getChildrenOf(parent, true, true, true);
List<MediaFile> mediaFiles = new ArrayList<MediaFile>();
for (int index : indexes) {
mediaFiles.add(allChildren.get(index));
}
for (MediaFile mediaFile : mediaFiles) {
zip(out, mediaFile.getParentFile(), mediaFile.getFile(), status, null);
}
out.close();
LOG.info("Downloaded '" + zipFileName + "' to " + status.getPlayer());
}
示例2: createArchiveOutputStream
import org.apache.tools.zip.ZipOutputStream; //导入方法依赖的package包/类
public ZipOutputStream createArchiveOutputStream(File destination) throws IOException {
ZipOutputStream outStream = new ZipOutputStream(destination);
try {
outStream.setUseZip64(zip64Mode);
outStream.setMethod(entryCompressionMethod);
return outStream;
} catch (Exception e) {
IOUtils.closeQuietly(outStream);
String message = String.format("Unable to create ZIP output stream for file %s.", destination);
throw new UncheckedIOException(message, e);
}
}
示例3: createArchiveOutputStream
import org.apache.tools.zip.ZipOutputStream; //导入方法依赖的package包/类
public ZipOutputStream createArchiveOutputStream(File destination) {
try {
ZipOutputStream outStream = new ZipOutputStream(destination);
outStream.setUseZip64(zip64Mode);
outStream.setMethod(entryCompressionMethod);
return outStream;
} catch (Exception e) {
String message = String.format("Unable to create ZIP output stream for file %s.", destination);
throw new UncheckedIOException(message, e);
}
}
示例4: downloadDirectory
import org.apache.tools.zip.ZipOutputStream; //导入方法依赖的package包/类
/**
* Downloads all files in a directory (including sub-directories). The files are packed together in an
* uncompressed zip-file.
*
* @param response The HTTP response.
* @param status The download status.
* @param file The file to download.
* @param range The byte range, may be <code>null</code>.
* @throws IOException If an I/O error occurs.
*/
private void downloadDirectory(HttpServletResponse response, TransferStatus status, File file, LongRange range) throws IOException {
String zipFileName = file.getName() + ".zip";
LOG.info("Starting to download '" + zipFileName + "' to " + status.getPlayer());
response.setContentType("application/x-download");
response.setHeader("Content-Disposition", "attachment; filename*=UTF-8''"+ encodeAsRFC5987(zipFileName));
ZipOutputStream out = new ZipOutputStream(RangeOutputStream.wrap(response.getOutputStream(), range));
out.setMethod(ZipOutputStream.STORED); // No compression.
zip(out, file.getParentFile(), file, status, range);
out.close();
LOG.info("Downloaded '" + zipFileName + "' to " + status.getPlayer());
}
示例5: zip
import org.apache.tools.zip.ZipOutputStream; //导入方法依赖的package包/类
/**
* 压缩文件夹或文件
*
* @param src
* src
* @param archive
* archive
* @param comment
* 压缩包注释
* @throws FileNotFoundException
* FileNotFoundException
* @throws IOException
* IOException
*/
public static void zip(String src, String archive, String comment)
throws FileNotFoundException, IOException {
// ----压缩文件:
FileOutputStream f = new FileOutputStream(archive);
// 使用指定校验和创建输出流
CheckedOutputStream csum = new CheckedOutputStream(f, new CRC32());
ZipOutputStream zos = new ZipOutputStream(csum);
// 支持中文
zos.setEncoding(encoding);
BufferedOutputStream out = new BufferedOutputStream(zos);
// 设置压缩包注释
zos.setComment(comment);
// 启用压缩
zos.setMethod(ZipOutputStream.DEFLATED);
// 压缩级别为最强压缩,但时间要花得多一点
zos.setLevel(Deflater.BEST_COMPRESSION);
File srcFile = new File(src);
if (!srcFile.exists() || (srcFile.isDirectory() && srcFile.list().length == 0)) {
out.close();
throw new FileNotFoundException(
"File must exist and ZIP file must have at least one entry.");
}
// 获取压缩源所在父目录
src = src.replaceAll("\\\\", "/");
String prefixDir = null;
if (srcFile.isFile()) {
prefixDir = src.substring(0, src.lastIndexOf("/") + 1);
} else {
prefixDir = (src.replaceAll("/$", "") + "/");
}
// 如果不是根目录
if (prefixDir.indexOf("/") != (prefixDir.length() - 1) && isCreateSrcDir) {
prefixDir = prefixDir.replaceAll("[^/]+/$", "");
}
// 开始压缩
saveZipFile(zos, out, srcFile, prefixDir);
out.close();
// 注:校验和要在流关闭后才准备,一定要放在流被关闭后使用
logger.debug("Checksum: " + csum.getChecksum().getValue());
}