java.util.zip.ZipOutputStream.<init>()方法的使用及代码示例

x33g5p2x  于2022-02-05 转载在 其他  
字(8.2k)|赞(0)|评价(0)|浏览(131)

本文整理了Java中java.util.zip.ZipOutputStream.<init>()方法的一些代码示例,展示了ZipOutputStream.<init>()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。ZipOutputStream.<init>()方法的具体详情如下:
包路径:java.util.zip.ZipOutputStream
类名称:ZipOutputStream
方法名:<init>

ZipOutputStream.<init>介绍

[英]Constructs a new ZipOutputStream that writes a zip file to the given OutputStream.
[中]构造一个新的ZipoutStream,将zip文件写入给定的输出流。

代码示例

canonical example by Tabnine

public void zipFile(File srcFile, File zipFile) throws IOException {
 try (FileInputStream fis = new FileInputStream(srcFile);
   ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(zipFile))) {
  zos.putNextEntry(new ZipEntry(srcFile.getName()));
  int len;
  byte[] buffer = new byte[1024];
  while ((len = fis.read(buffer)) > 0) {
   zos.write(buffer, 0, len);
  }
  zos.closeEntry();
 }
}

代码示例来源: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: stackoverflow.com

FileOutputStream fout = new FileOutputStream("test.zip");
ZipOutputStream zout = new ZipOutputStream(fout);
for all files
{
  ZipEntry ze = new ZipEntry(filename);
  zout.putNextEntry(ze);
  send data to zout;
  zout.closeEntry();
}
zout.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: Tencent/tinker

zipOutputStream = new ZipOutputStream(new FileOutputStream(outputZipFullFilename));
if (inputZipFullFilename != null) {
  zipFile = new ZipFile(inputZipFullFilename);
    ZipEntry newZipEntry = new ZipEntry(zipEntryName);
    addZipEntry(zipOutputStream, newZipEntry, inputStream);

代码示例来源:origin: apache/ignite

/**
 * Archives specified file into zip archive.
 *
 * @param file File to be archived.
 * @return Byte array representing zip archive.
 * @throws IOException In case of input/output exception.
 */
private byte[] zipFile(File file) throws IOException {
  ByteArrayOutputStream baos = new ByteArrayOutputStream();
  try (ZipOutputStream zos = new ZipOutputStream(baos)) {
    ZipEntry entry = new ZipEntry(file.getName());
    zos.putNextEntry(entry);
    try (FileInputStream in = new FileInputStream(file.getAbsolutePath())) {
      IOUtils.copy(in, zos);
    }
  }
  return baos.toByteArray();
}

代码示例来源: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: redisson/redisson

protected ZipBuilder(File zipFile) throws IOException {
  if (!FileUtil.isExistingFile(zipFile)) {
    FileUtil.touch(zipFile);
  }
  zos = new ZipOutputStream(new FileOutputStream(zipFile));
  targetZipFile = zipFile;
  targetBaos = null;
}

代码示例来源:origin: pentaho/pentaho-kettle

private ZipInputStream createZIPInputStream() throws IOException {
  // Create an in-memory ZIP output stream for use by the input stream (to avoid exceptions)
  ByteArrayOutputStream baos = new ByteArrayOutputStream();
  ZipOutputStream gos = new ZipOutputStream( baos );
  gos.putNextEntry( new ZipEntry( "./test.txt" ) );
  byte[] testBytes = "Test".getBytes();
  gos.write( testBytes );
  ByteArrayInputStream in = new ByteArrayInputStream( baos.toByteArray() );

  return new ZipInputStream( in );
 }
}

代码示例来源: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: gocd/gocd

private void backupConfigRepository(File backupDir) throws IOException {
  File configRepoDir = systemEnvironment.getConfigRepoDir();
  try (ZipOutputStream configRepoZipStream = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(new File(backupDir, CONFIG_REPOSITORY_BACKUP_ZIP))))) {
    new DirectoryStructureWalker(configRepoDir.getAbsolutePath(), configRepoZipStream).walk();
  }
}

代码示例来源:origin: pxb1988/dex2jar

public void dumpZip(Path exFile, String[] originalArgs) throws IOException {
  try (ZipOutputStream zos = new ZipOutputStream(Files.newOutputStream(exFile))) {
    BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(zos, StandardCharsets.UTF_8));
    zos.putNextEntry(new ZipEntry("summary.txt"));
    dumpTxt0(writer, originalArgs);
    zos.closeEntry();
    zos.flush();
  }
}

代码示例来源: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: oblac/jodd

protected ZipBuilder(final File zipFile) throws IOException {
  if (!FileUtil.isExistingFile(zipFile)) {
    FileUtil.touch(zipFile);
  }
  zos = new ZipOutputStream(new FileOutputStream(zipFile));
  targetZipFile = zipFile;
  targetBaos = null;
}

代码示例来源: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();
 }
}

相关文章