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

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

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

ZipOutputStream.closeEntry介绍

[英]Closes the current ZipEntry. Any entry terminal data is written to the underlying stream.
[中]关闭当前的ZipEntry。任何输入终端数据都会写入底层流。

代码示例

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: SonarSource/sonarqube

private static void doZip(String entryName, InputStream in, ZipOutputStream out) throws IOException {
 ZipEntry entry = new ZipEntry(entryName);
 out.putNextEntry(entry);
 IOUtils.copy(in, out);
 out.closeEntry();
}

代码示例来源:origin: spotbugs/spotbugs

void notBug(ZipOutputStream any, ZipEntry anyZipEntry, int anyValue) throws IOException {
  any.putNextEntry(anyZipEntry);
  any.write(anyValue);
  any.closeEntry();
}

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

private static void copyToZipStream(InputStream is, ZipEntry entry, ZipOutputStream zos)
  throws IOException {
 zos.putNextEntry(entry);
 IOUtils.copy(is, zos);
 is.close();
 zos.closeEntry();
}

代码示例来源:origin: apache/incubator-druid

public static long copyFileToZipStream(
  File file,
  ZipOutputStream zipOutputStream,
  Progressable progressable
) throws IOException
{
 createNewZipEntry(zipOutputStream, file);
 long numRead = 0;
 try (FileInputStream inputStream = new FileInputStream(file)) {
  byte[] buf = new byte[0x10000];
  for (int bytesRead = inputStream.read(buf); bytesRead >= 0; bytesRead = inputStream.read(buf)) {
   progressable.progress();
   if (bytesRead == 0) {
    continue;
   }
   zipOutputStream.write(buf, 0, bytesRead);
   progressable.progress();
   numRead += bytesRead;
  }
 }
 zipOutputStream.closeEntry();
 progressable.progress();
 return numRead;
}

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

public void testZip(ZipOutputStream zos) throws Exception {
  ZipEntry ze = new ZipEntry("foo");
  zos.putNextEntry(ze);
  zos.closeEntry();
}

代码示例来源:origin: ankidroid/Anki-Android

private void writeEntry(BufferedInputStream bis, ZipEntry ze) throws IOException {
  byte[] buf = new byte[BUFFER_SIZE];
  mZos.putNextEntry(ze);
  int len;
  while ((len = bis.read(buf, 0, BUFFER_SIZE)) != -1) {
    mZos.write(buf, 0, len);
  }
  mZos.closeEntry();
  bis.close();
}

代码示例来源:origin: Meituan-Dianping/Robust

protected void zipFile(byte[] classBytesArray, ZipOutputStream zos, String entryName) {
    try {
      ZipEntry entry = new ZipEntry(entryName);
      zos.putNextEntry(entry);
      zos.write(classBytesArray, 0, classBytesArray.length);
      zos.closeEntry();
      zos.flush();
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
}

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

private boolean createDir0(String path) throws IOException {
  int x = path.lastIndexOf('/', path.length() - 2);
  if (x > 0) {
    String n = path.substring(0, x + 1);
    createDir0(n);
  }
  if (!path.contains(path)) {
    files.add(path);
    ZipEntry zipEntry = new ZipEntry(path);
    zos.putNextEntry(zipEntry);
    zos.closeEntry();
    return true;
  }
  return false;
}

代码示例来源:origin: Tencent/tinker

/**
 * add zip entry
 *
 * @param zipOutputStream
 * @param zipEntry
 * @param inputStream
 * @throws Exception
 */
public static void addZipEntry(ZipOutputStream zipOutputStream, ZipEntry zipEntry, InputStream inputStream) throws Exception {
  try {
    zipOutputStream.putNextEntry(zipEntry);
    byte[] buffer = new byte[Constant.Capacity.BYTES_PER_KB];
    int length = -1;
    while ((length = inputStream.read(buffer, 0, buffer.length)) != -1) {
      zipOutputStream.write(buffer, 0, length);
      zipOutputStream.flush();
    }
  } catch (ZipException e) {
    // do nothing
  } finally {
    StreamUtil.closeQuietly(inputStream);
    zipOutputStream.closeEntry();
  }
}

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

private void copyExistingFiles(ZipFile inputFile, ZipOutputStream outputFile) throws IOException {
  // First, copy the contents from the existing outFile:
  Enumeration<? extends ZipEntry> entries = inputFile.entries();
  while (entries.hasMoreElements()) {
    ZipEntry entry = new ZipEntry(entries.nextElement());
    // We can't reuse the compressed size because it depends on compression sizes.
    entry.setCompressedSize(-1);
    outputFile.putNextEntry(entry);
    // No need to create directory entries in the final apk
    if (! entry.isDirectory()) {
      BrutIO.copy(inputFile, outputFile, entry);
    }
    outputFile.closeEntry();
  }
}

代码示例来源:origin: org.apache.ant/ant

private void addToOutputStream(ZipOutputStream output, InputStream input,
                ZipEntry ze) throws IOException {
  try {
    output.putNextEntry(ze);
  } catch (ZipException zipEx) {
    //This entry already exists. So, go with the first one.
    input.close();
    return;
  }
  int numBytes;
  while ((numBytes = input.read(buffer)) > 0) {
    output.write(buffer, 0, numBytes);
  }
  output.closeEntry();
  input.close();
}

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

private static void backupFile(ZipOutputStream out, InputStream in, String entryName) throws IOException {
    out.putNextEntry(new ZipEntry(entryName));
    IOUtils.copyAndCloseInput(in, out);
    out.closeEntry();
  }
}

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

private static void copyToZipStream(File file, ZipEntry entry,
              ZipOutputStream zos) throws IOException {
 InputStream is = new FileInputStream(file);
 try {
  zos.putNextEntry(entry);
  byte[] arr = new byte[4096];
  int read = is.read(arr);
  while (read > -1) {
   zos.write(arr, 0, read);
   read = is.read(arr);
  }
 } finally {
  try {
   is.close();
  } finally {
   zos.closeEntry();
  }
 }
}

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

public static void addFolderToZip(ZipOutputStream zos, String path, String comment) throws IOException {
  while (path.length() != 0 && path.charAt(0) == '/') {
    path = path.substring(1);
  }
  // add folder record
  if (!StringUtil.endsWithChar(path, '/')) {
    path += '/';
  }
  ZipEntry zipEntry = new ZipEntry(path);
  zipEntry.setTime(System.currentTimeMillis());
  if (comment != null) {
    zipEntry.setComment(comment);
  }
  zipEntry.setSize(0);
  zipEntry.setCrc(0);
  zos.putNextEntry(zipEntry);
  zos.closeEntry();
}

相关文章