本文整理了Java中java.util.zip.ZipOutputStream.close()
方法的一些代码示例,展示了ZipOutputStream.close()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。ZipOutputStream.close()
方法的具体详情如下:
包路径:java.util.zip.ZipOutputStream
类名称:ZipOutputStream
方法名:close
[英]Closes the current ZipEntry, if any, and the underlying output stream. If the stream is already closed this method does nothing.
[中]关闭当前ZipEntry(如果有)和底层输出流。如果流已关闭,则此方法不会执行任何操作。
代码示例来源:origin: apache/incubator-druid
public static void makeEvilZip(File outputFile) throws IOException
{
ZipOutputStream zipOutputStream = new ZipOutputStream(new FileOutputStream(outputFile));
ZipEntry zipEntry = new ZipEntry("../../../../../../../../../../../../../../../tmp/evil.txt");
zipOutputStream.putNextEntry(zipEntry);
byte[] output = StringUtils.toUtf8("evil text");
zipOutputStream.write(output);
zipOutputStream.closeEntry();
zipOutputStream.close();
}
}
代码示例来源:origin: Blankj/AndroidUtilCode
/**
* Zip the file.
*
* @param srcFile The source of file.
* @param zipFile The ZIP file.
* @param comment The comment.
* @return {@code true}: success<br>{@code false}: fail
* @throws IOException if an I/O error has occurred
*/
public static boolean zipFile(final File srcFile,
final File zipFile,
final String comment)
throws IOException {
if (srcFile == null || zipFile == null) return false;
ZipOutputStream zos = null;
try {
zos = new ZipOutputStream(new FileOutputStream(zipFile));
return zipFile(srcFile, "", zos, comment);
} finally {
if (zos != null) {
zos.close();
}
}
}
代码示例来源:origin: gocd/gocd
public void done() throws IOException {
ZipOutputStream zip = null;
try {
zip = new ZipOutputStream(new BufferedOutputStream(destinationStream));
zip.setLevel(level);
for (Map.Entry<String, File> zipDirToSourceFileEntry : toAdd.entrySet()) {
File sourceFileToZip = zipDirToSourceFileEntry.getValue();
String destinationFolder = zipDirToSourceFileEntry.getKey();
zipUtil.addToZip(new ZipPath(destinationFolder), sourceFileToZip, zip, excludeRootDir);
}
zip.flush();
} finally {
if (zip != null) {
try {
zip.close();
} catch (IOException e) {
LOGGER.error("Failed to close the stream", e);
}
}
}
}
}
代码示例来源:origin: stackoverflow.com
StringBuilder sb = new StringBuilder();
sb.append("Test String");
File f = new File("d:\\test.zip");
ZipOutputStream out = new ZipOutputStream(new FileOutputStream(f));
ZipEntry e = new ZipEntry("mytext.txt");
out.putNextEntry(e);
byte[] data = sb.toString().getBytes();
out.write(data, 0, data.length);
out.closeEntry();
out.close();
代码示例来源:origin: iBotPeaches/Apktool
public static void zipFolders(final File folder, final File zip, final File assets, final Collection<String> doNotCompress)
throws BrutException, IOException {
mDoNotCompress = doNotCompress;
ZipOutputStream zipOutputStream = new ZipOutputStream(new FileOutputStream(zip));
zipFolders(folder, zipOutputStream);
// We manually set the assets because we need to retain the folder structure
if (assets != null) {
processFolder(assets, zipOutputStream, assets.getPath().length() - 6);
}
zipOutputStream.close();
}
代码示例来源:origin: zeroturnaround/zt-zip
private static void pack(ZipEntrySource[] entries, OutputStream os, boolean closeStream) {
try {
ZipOutputStream out = new ZipOutputStream(os);
for (int i = 0; i < entries.length; i++) {
addEntry(entries[i], out);
}
out.flush();
out.finish();
if (closeStream) {
out.close();
}
}
catch (IOException e) {
throw ZipExceptionUtil.rethrow(e);
}
}
代码示例来源:origin: spotbugs/spotbugs
@ExpectWarning("AM_CREATES_EMPTY_ZIP_FILE_ENTRY")
public static void main(String args[]) throws Exception {
ZipOutputStream zipfile = new ZipOutputStream(new FileOutputStream("foo.zip"));
for (int i = 0; i < args.length; i++) {
ZipEntry e = new ZipEntry(args[i]);
zipfile.putNextEntry(e);
zipfile.closeEntry();
}
zipfile.close();
}
}
代码示例来源:origin: apache/hive
/**
* Testing related; creates an empty archive to served being localized as hive-exec
*/
private URI createEmptyArchive() {
try {
File outputJar = new File(System.getProperty("build.test.dir"), "empty.jar");
ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(outputJar));
zos.close();
return outputJar.toURI();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
代码示例来源:origin: h2oai/h2o-2
@Override public void lcompute() {
_result = new byte[H2O.CLOUD._memary.length][];
int idx = H2O.SELF.index();
baos = new ByteArrayOutputStream();
ZipOutputStream zos = new ZipOutputStream(baos);
try {
zipDir(Log.LOG_DIR, zos);
}
catch (IOException e) {
H2O.ignore(e);
}
finally {
try {
zos.close();
baos.close();
} catch (Exception xe) {
// do nothing
}
byte[] arr = baos.toByteArray();
_result[idx] = arr;
tryComplete();
}
}
代码示例来源:origin: opentripplanner/OpenTripPlanner
@Override
public void write(OutputStream outStream) throws IOException {
ZipOutputStream zip = new ZipOutputStream(outStream);
for (File f : directory.listFiles()) {
zip.putNextEntry(new ZipEntry(f.getName()));
Files.copy(f, zip);
zip.closeEntry();
zip.flush();
}
zip.close();
}
}
代码示例来源:origin: Tencent/tinker
/**
* zip list of file
*
* @param resFileList file(dir) list
* @param zipFile output zip file
* @throws IOException
*/
public static void zipFiles(Collection<File> resFileList, File zipFile, String comment) throws IOException {
ZipOutputStream zipout = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(zipFile), TypedValue.BUFFER_SIZE));
for (File resFile : resFileList) {
if (resFile.exists()) {
zipFile(resFile, zipout, "");
}
}
if (comment != null) {
zipout.setComment(comment);
}
zipout.close();
}
代码示例来源:origin: square/wire
@Test public void profileInZip() throws IOException {
FileSystem fileSystem = Jimfs.newFileSystem(Configuration.unix());
Files.createDirectories(fileSystem.getPath("/source"));
Path zip = fileSystem.getPath("/source/protos.zip");
ZipOutputStream zipOutputStream = new ZipOutputStream(Files.newOutputStream(zip));
writeFile(zipOutputStream, "a/b/message.proto", ""
+ "package a.b;\n"
+ "message Message {"
+ "}");
writeFile(zipOutputStream, "a/b/android.wire", ""
+ "syntax = \"wire2\";\n"
+ "package a.b;\n"
+ "import \"a/b/message.proto\";\n"
+ "type a.b.Message {\n"
+ " target java.lang.Object using com.example.Message#ADAPTER;\n"
+ "}");
zipOutputStream.close();
Schema schema = new SchemaLoader()
.addSource(zip)
.load();
Profile profile = new ProfileLoader(fileSystem, "android")
.schema(schema)
.load();
ProtoType message = ProtoType.get("a.b.Message");
assertThat(profile.getTarget(message)).isEqualTo(ClassName.OBJECT);
assertThat(profile.getAdapter(message)).isEqualTo(
new AdapterConstant("com.example.Message#ADAPTER"));
}
代码示例来源:origin: javamelody/javamelody
public static void zipFile(File source, File target) throws IOException {
final FileOutputStream fos = new FileOutputStream(target);
final ZipOutputStream zos = new ZipOutputStream(fos);
try {
final ZipEntry ze = new ZipEntry(source.getName());
zos.putNextEntry(ze);
pumpFromFile(source, zos);
zos.closeEntry();
} finally {
zos.close();
}
}
代码示例来源:origin: azkaban/azkaban
public static void zip(final File input, final File output) throws IOException {
final FileOutputStream out = new FileOutputStream(output);
final ZipOutputStream zOut = new ZipOutputStream(out);
try {
zipFile("", input, zOut);
} finally {
zOut.close();
}
}
代码示例来源:origin: opentripplanner/OpenTripPlanner
@Override
public void write(OutputStream outStream) throws IOException {
ZipOutputStream zip = new ZipOutputStream(outStream);
for (File f : directory.listFiles()) {
zip.putNextEntry(new ZipEntry(f.getName()));
Files.copy(f, zip);
zip.closeEntry();
zip.flush();
}
zip.close();
}
}
代码示例来源:origin: ZHENFENG13/My-Blog
public static void zipFolder(String srcFolder, String destZipFile) throws Exception {
ZipOutputStream zip = null;
FileOutputStream fileWriter = null;
fileWriter = new FileOutputStream(destZipFile);
zip = new ZipOutputStream(fileWriter);
addFolderToZip("", srcFolder, zip);
zip.flush();
zip.close();
}
代码示例来源:origin: ZHENFENG13/My-Blog
public static void zipFile(String filePath, String zipPath) throws Exception{
byte[] buffer = new byte[1024];
FileOutputStream fos = new FileOutputStream(zipPath);
ZipOutputStream zos = new ZipOutputStream(fos);
ZipEntry ze= new ZipEntry("spy.log");
zos.putNextEntry(ze);
FileInputStream in = new FileInputStream(filePath);
int len;
while ((len = in.read(buffer)) > 0) {
zos.write(buffer, 0, len);
}
in.close();
zos.closeEntry();
//remember close it
zos.close();
}
代码示例来源:origin: vipshop/Saturn
public static void zip(List<File> runtimeLibFiles, File saturnContainerDir, File zipFile) throws IOException {
ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(zipFile));
/*
* for(File file : saturnContainerDir.listFiles()) { zip(file, "saturn", zos); }
*/
for (File file : runtimeLibFiles) {
zip(file, "app/lib", zos);
}
zos.close();
}
代码示例来源:origin: stackoverflow.com
public static byte[] zipBytes(String filename, byte[] input) throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ZipOutputStream zos = new ZipOutputStream(baos);
ZipEntry entry = new ZipEntry(filename);
entry.setSize(input.length);
zos.putNextEntry(entry);
zos.write(input);
zos.closeEntry();
zos.close();
return baos.toByteArray();
}
代码示例来源:origin: Blankj/AndroidUtilCode
/**
* Zip the files.
*
* @param srcFiles The source of files.
* @param zipFile The ZIP file.
* @param comment The comment.
* @return {@code true}: success<br>{@code false}: fail
* @throws IOException if an I/O error has occurred
*/
public static boolean zipFiles(final Collection<File> srcFiles,
final File zipFile,
final String comment)
throws IOException {
if (srcFiles == null || zipFile == null) return false;
ZipOutputStream zos = null;
try {
zos = new ZipOutputStream(new FileOutputStream(zipFile));
for (File srcFile : srcFiles) {
if (!zipFile(srcFile, "", zos, comment)) return false;
}
return true;
} finally {
if (zos != null) {
zos.finish();
zos.close();
}
}
}
内容来源于网络,如有侵权,请联系作者删除!