本文整理汇总了Java中org.apache.tools.zip.ZipOutputStream类的典型用法代码示例。如果您正苦于以下问题:Java ZipOutputStream类的具体用法?Java ZipOutputStream怎么用?Java ZipOutputStream使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
ZipOutputStream类属于org.apache.tools.zip包,在下文中一共展示了ZipOutputStream类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: zipFile
import org.apache.tools.zip.ZipOutputStream; //导入依赖的package包/类
@Override
protected void zipFile(File file, ZipOutputStream zOut, String vPath, int mode) throws IOException {
if (vPath.equals(layer)) {
System.setProperty("CslJar", Boolean.TRUE.toString());
try {
// Create a tempfile and trick it!
InputStream is = new FileInputStream(file);
String modifiedLayer = getModifiedLayer(is);
if (modifiedLayer != null) {
File tmpFile = File.createTempFile("csl", "tmp"); // NOI18N
BufferedWriter w = new BufferedWriter(new FileWriter(tmpFile));
w.write(modifiedLayer);
w.flush();
w.close();
// Note - we're passing the temp file instead of the "real" layer file
super.zipFile(tmpFile, zOut, vPath, mode);
// Remove the tmpfile
tmpFile.delete();
return;
}
} finally {
System.setProperty("CslJar", Boolean.FALSE.toString());
}
}
super.zipFile(file, zOut, vPath, mode);
}
示例2: writeTo
import org.apache.tools.zip.ZipOutputStream; //导入依赖的package包/类
public void writeTo(OutputStream out) throws IOException {
final Set<String> filenames = new HashSet<String>();
final ZipOutputStream zipout = new ZipOutputStream(out);
for (IFile f : container.getFiles()) {
assertNoAbsolutePath(f);
assertNoDuplicates(filenames, f);
ZipEntry entry = new ZipEntry(f.getLocalPath());
entry.setTime(f.getLastModified());
if (f.getPermissions() != IFile.UNDEF_PERMISSIONS) {
entry.setUnixMode(f.getPermissions());
}
zipout.putNextEntry(entry);
f.writeTo(zipout);
zipout.closeEntry();
}
zipout.finish();
}
示例3: zip
import org.apache.tools.zip.ZipOutputStream; //导入依赖的package包/类
/**
* <p>
* 压缩文件
* </p>
*
* @param sourceFolder 压缩文件夹
* @param zipFilePath 压缩文件输出路径
*/
public static void zip(String sourceFolder, String zipFilePath) throws Exception {
OutputStream out = new FileOutputStream(zipFilePath);
BufferedOutputStream bos = new BufferedOutputStream(out);
ZipOutputStream zos = new ZipOutputStream(bos);
// 解决中文文件名乱码
zos.setEncoding(CHINESE_CHARSET);
File file = new File(sourceFolder);
String basePath = null;
if (file.isDirectory()) {
basePath = file.getPath();
} else {
basePath = file.getParent();
}
zipFile(file, basePath, zos);
zos.closeEntry();
zos.close();
bos.close();
out.close();
}
示例4: doZip
import org.apache.tools.zip.ZipOutputStream; //导入依赖的package包/类
private static void doZip(ZipOutputStream zos, String filePath, String pathName) throws IOException {
File file2zip = new File(filePath);
if (file2zip.isFile()) {
zos.putNextEntry(new org.apache.tools.zip.ZipEntry(pathName + file2zip.getName()));
IOUtils.copy(new FileInputStream(file2zip.getAbsolutePath()), zos);
zos.closeEntry();
} else {
File[] files = file2zip.listFiles();
if (files != null) {
for (File f : files) {
if (f.isDirectory()) {
doZip(zos, f.getAbsolutePath(), pathName + f.getName() + File.separator);
} else {
zos.putNextEntry(new org.apache.tools.zip.ZipEntry(pathName + File.separator + f.getName()));
IOUtils.copy(new FileInputStream(f.getAbsolutePath()), zos);
zos.closeEntry();
}
}
}
}
}
示例5: zipFile
import org.apache.tools.zip.ZipOutputStream; //导入依赖的package包/类
@Override
protected void zipFile(InputStream is, ZipOutputStream zOut, String vPath, long lastModified, File fromArchive, int mode) throws IOException
{
if (vPath.startsWith("META-INF/"))
{
if (!vPath.equals("META-INF/"+applicationName+".crcver"))
{
super.zipFile(is, zOut, vPath, lastModified, fromArchive, mode);
}
}
else
{
crcInputStream.resetStream(is);
super.zipFile(crcInputStream, zOut, vPath, lastModified, fromArchive, mode);
fileChecksums.add(new FileChecksum(vPath, crcInputStream.getCrcValue()));
}
}
示例6: canDecompressZipFile
import org.apache.tools.zip.ZipOutputStream; //导入依赖的package包/类
@Test
public void canDecompressZipFile() {
try {
compressedFile = Files.createTempFile(ARCHIVE_PREFIX, ".zip");
try (final ZipArchiveOutputStream outputStream = new ZipArchiveOutputStream(
new BufferedOutputStream(new FileOutputStream(compressedFile.toFile())))) {
// Deflated is the default compression method
zipDirectory(testDir, outputStream, ZipOutputStream.DEFLATED);
}
ExtractionTools.decompressFile(
compressedFile.toFile(),
decompressDestination.toFile(),
CompressionType.Zip,
null);
assertEquals(getFileNames(testDir), getFileNames(decompressDestination));
} catch (final IOException e) {
fail(e.getMessage());
}
}
示例7: canDecompressZipFileWithStoredCompressionMethod
import org.apache.tools.zip.ZipOutputStream; //导入依赖的package包/类
@Test
public void canDecompressZipFileWithStoredCompressionMethod() {
try {
compressedFile = Files.createTempFile(ARCHIVE_PREFIX, ".zip");
try (final ZipArchiveOutputStream outputStream = new ZipArchiveOutputStream(
new BufferedOutputStream(new FileOutputStream(compressedFile.toFile())))) {
zipDirectory(testDir, outputStream, ZipOutputStream.STORED);
}
ExtractionTools.decompressFile(
compressedFile.toFile(),
decompressDestination.toFile(),
CompressionType.Zip,
null);
assertEquals(getFileNames(testDir), getFileNames(decompressDestination));
} catch (final IOException e) {
fail(e.getMessage());
}
}
示例8: createZipOutputStream
import org.apache.tools.zip.ZipOutputStream; //导入依赖的package包/类
@Test
@BenchmarkOptions(benchmarkRounds = 1, warmupRounds = 0, concurrency = 1)
public void createZipOutputStream() throws Exception {
final String FILE_NAME = "custom/output/zipOutputStream.zip";
//
ZipOutputStream value = IoHelper.createZipOutputStream(FILE_NAME);
System.out.println(value);
assertNotNull(value);
//
String fileName = "custom/output/test.log";
byte[] contents = IoHelper.read(fileName);
ZipEntry zipEntry = new ZipEntry(fileName);
value.putNextEntry(zipEntry);
value.write(contents);
// 須加 close,強制寫入
IoHelper.close(value);
}
示例9: zipFile
import org.apache.tools.zip.ZipOutputStream; //导入依赖的package包/类
@Override
protected void zipFile(InputStream is, ZipOutputStream zOut, String vPath,
long lastModified, File fromArchive, int mode) throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
IoUtil.copy(is, baos, buf);
struct.data = baos.toByteArray();
struct.name = vPath;
struct.time = lastModified;
if (proc.process(struct) != JarProcessor.Result.DISCARD) {
if (mode == 0)
mode = ZipFileSet.DEFAULT_FILE_MODE;
if (!filesOnly) {
addParentDirs(struct.name, zOut);
}
super.zipFile(new ByteArrayInputStream(struct.data),
zOut, struct.name, struct.time, fromArchive, mode);
}
}
示例10: zip
import org.apache.tools.zip.ZipOutputStream; //导入依赖的package包/类
public static void zip(String path, List<File> files) throws IOException {
ZipOutputStream zos = new ZipOutputStream(new BufferedOutputStream(
new FileOutputStream(path), 1024));
for(File f : files) {
String zipName = f.getName();
if(!f.getName().contains(".png")) {
zipName = f.getName() + ".xml";
}
ZipEntry ze = new ZipEntry(zipName);
ze.setTime(f.lastModified());
DataInputStream dis = new DataInputStream(new BufferedInputStream(
new FileInputStream(f)));
zos.putNextEntry(ze);
int c;
while ((c = dis.read()) != -1) {
zos.write(c);
}
}
zos.setEncoding("gbk");
zos.closeEntry();
zos.close();
}
示例11: closeZout
import org.apache.tools.zip.ZipOutputStream; //导入依赖的package包/类
/** Close zout */
private void closeZout(final ZipOutputStream zOut, final boolean success)
throws IOException {
if (zOut == null) {
return;
}
try {
zOut.close();
} catch (final IOException ex) {
// If we're in this finally clause because of an
// exception, we don't really care if there's an
// exception when closing the stream. E.g. if it
// throws "ZIP file must have at least one entry",
// because an exception happened before we added
// any files, then we must swallow this
// exception. Otherwise, the error that's reported
// will be the close() error, which is not the
// real cause of the problem.
if (success) {
throw ex;
}
}
}
示例12: addDirectoryResource
import org.apache.tools.zip.ZipOutputStream; //导入依赖的package包/类
/**
* Add a directory entry to the archive using a specified
* Unix-mode and the default mode for its parent directories (if
* necessary).
*/
private void addDirectoryResource(final Resource r, String name, final String prefix,
final File base, final ZipOutputStream zOut,
final int defaultDirMode, final int thisDirMode)
throws IOException {
if (!name.endsWith("/")) {
name = name + "/";
}
final int nextToLastSlash = name.lastIndexOf('/', name.length() - 2);
if (nextToLastSlash != -1) {
addParentDirs(base, name.substring(0, nextToLastSlash + 1),
zOut, prefix, defaultDirMode);
}
zipDir(r, zOut, prefix + name, thisDirMode,
r instanceof ZipResource
? ((ZipResource) r).getExtraFields() : null);
}
示例13: zipFile
import org.apache.tools.zip.ZipOutputStream; //导入依赖的package包/类
/**
* Method that gets called when adding from <code>java.io.File</code> instances.
*
* <p>This implementation delegates to the six-arg version.</p>
*
* @param file the file to add to the archive
* @param zOut the stream to write to
* @param vPath the name this entry shall have in the archive
* @param mode the Unix permissions to set.
* @throws IOException on error
*
* @since Ant 1.5.2
*/
protected void zipFile(final File file, final ZipOutputStream zOut, final String vPath,
final int mode)
throws IOException {
if (file.equals(zipFile)) {
throw new BuildException("A zip file cannot include itself",
getLocation());
}
try (final BufferedInputStream bIn = new BufferedInputStream(Files.newInputStream(file.toPath()))) {
// ZIPs store time with a granularity of 2 seconds, round up
zipFile(bIn, zOut, vPath,
file.lastModified() + (roundUp ? ROUNDUP_MILLIS : 0),
null, mode);
}
}
示例14: zipFile
import org.apache.tools.zip.ZipOutputStream; //导入依赖的package包/类
/**
* Overridden from Zip class to deal with manifests and index lists.
* @param is the stream to read data for the entry from. The
* caller of the method is responsible for closing the stream.
* @param zOut the zip output stream
* @param vPath the name this entry shall have in the archive
* @param lastModified last modification time for the entry.
* @param fromArchive the original archive we are copying this
* entry from, will be null if we are not copying from an archive.
* @param mode the Unix permissions to set.
* @throws IOException on error
*/
@Override
protected void zipFile(InputStream is, ZipOutputStream zOut, String vPath,
long lastModified, File fromArchive, int mode)
throws IOException {
if (MANIFEST_NAME.equalsIgnoreCase(vPath)) {
if (isFirstPass()) {
filesetManifest(fromArchive, is);
}
} else if (INDEX_NAME.equalsIgnoreCase(vPath) && index) {
logWhenWriting("Warning: selected " + archiveType
+ " files include a " + INDEX_NAME + " which will"
+ " be replaced by a newly generated one.",
Project.MSG_WARN);
} else {
if (index && vPath.indexOf('/') == -1) {
rootEntries.add(vPath);
}
super.zipFile(is, zOut, vPath, lastModified, fromArchive, mode);
}
}
示例15: getSetPermissionsWorksForZipResources
import org.apache.tools.zip.ZipOutputStream; //导入依赖的package包/类
@Test
public void getSetPermissionsWorksForZipResources() throws IOException {
File f = File.createTempFile("ant", ".zip");
f.deleteOnExit();
try (ZipOutputStream os = new ZipOutputStream(f)) {
ZipEntry e = new ZipEntry("foo");
os.putNextEntry(e);
os.closeEntry();
}
ZipResource r = new ZipResource();
r.setName("foo");
r.setArchive(f);
Set<PosixFilePermission> s =
EnumSet.of(PosixFilePermission.OWNER_READ,
PosixFilePermission.OWNER_WRITE,
PosixFilePermission.OWNER_EXECUTE,
PosixFilePermission.GROUP_READ);
PermissionUtils.setPermissions(r, s, null);
assertEquals(s, PermissionUtils.getPermissions(r, null));
}