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

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

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

ZipOutputStream.write介绍

[英]Writes data for the current entry to the underlying stream.
[中]将当前项的数据写入基础流。

代码示例

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: spotbugs/spotbugs

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

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

void addToZip(ZipPath path, File srcFile, ZipOutputStream zip, boolean excludeRootDir) throws IOException {
  if (srcFile.isDirectory()) {
    addFolderToZip(path, srcFile, zip, excludeRootDir);
  } else {
    byte[] buff = new byte[4096];
    try (BufferedInputStream inputStream = new BufferedInputStream(new FileInputStream(srcFile))) {
      ZipEntry zipEntry = path.with(srcFile).asZipEntry();
      zipEntry.setTime(srcFile.lastModified());
      zip.putNextEntry(zipEntry);
      int len;
      while ((len = inputStream.read(buff)) > 0) {
        zip.write(buff, 0, len);
      }
    }
  }
}

代码示例来源:origin: square/wire

private void writeFile(ZipOutputStream out, String file, String content) throws IOException {
  out.putNextEntry(new ZipEntry(file));
  out.write(content.getBytes(UTF_8));
 }
}

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

@Override
public void saveClassEntry(String path, String archiveName, String qualifiedName, String entryName, String content) {
 String file = new File(getAbsolutePath(path), archiveName).getPath();
 if (!checkEntry(entryName, file)) {
  return;
 }
 try {
  ZipOutputStream out = mapArchiveStreams.get(file);
  out.putNextEntry(new ZipEntry(entryName));
  if (content != null) {
   out.write(content.getBytes(StandardCharsets.UTF_8));
  }
 }
 catch (IOException ex) {
  String message = "Cannot write entry " + entryName + " to " + file;
  DecompilerContext.getLogger().writeMessage(message, ex);
 }
}

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

private void addFileToZip(ZipOutputStream zos, String filename)
  throws IOException
{
  FileInputStream fis =
    new FileInputStream(new File(exportDirName + filename));
  ZipEntry ze = new ZipEntry(filename);
  zos.putNextEntry(ze);
  int length;
  byte bytes[] = new byte[1024];
  while ((length = fis.read(bytes)) >= 0) zos.write(bytes, 0, length);
  zos.closeEntry();
  fis.close();
}

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

@NoWarning("AM_CREATES_EMPTY_ZIP_FILE_ENTRY")
void notBug(ZipOutputStream any, ZipEntry anyZipEntry, byte[] anyBytes) throws IOException {
  any.putNextEntry(anyZipEntry);
  any.write(anyBytes);
  any.closeEntry();
}

代码示例来源:origin: stackoverflow.com

// simplified code for zip creation in java

import java.io.*;
import java.util.zip.*;

public class ZipCreateExample {

  public static void main(String[] args) throws Exception {

    // input file 
    FileInputStream in = new FileInputStream("F:/sometxt.txt");

    // out put file 
    ZipOutputStream out = new ZipOutputStream(new FileOutputStream("F:/tmp.zip"));

    // name the file inside the zip  file 
    out.putNextEntry(new ZipEntry("zippedjava.txt")); 

    // buffer size
    byte[] b = new byte[1024];
    int count;

    while ((count = in.read(b)) > 0) {
      out.write(b, 0, count);
    }
    out.close();
    in.close();
  }
}

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

@ExpectWarning("AM_CREATES_EMPTY_ZIP_FILE_ENTRY")
void bug3(ZipOutputStream any, ZipEntry anyZipEntry, int anyValue) throws IOException {
  any.write(anyValue);
  any.putNextEntry(anyZipEntry);
  any.closeEntry();
}

代码示例来源:origin: opentripplanner/OpenTripPlanner

private static void zipGraphDir(File graphDirectory, File zipGraphFile) throws IOException {
  
  FileOutputStream fileOutputStream = new FileOutputStream(zipGraphFile);
  ZipOutputStream zipOutputStream = new ZipOutputStream(fileOutputStream);
  
  byte[] buffer = new byte[1024];
  
  for(File f : graphDirectory.listFiles()) {
    if (f.isDirectory())
      continue;
    
    ZipEntry zipEntry = new ZipEntry(f.getName());
    zipOutputStream.putNextEntry(zipEntry);
    FileInputStream fileInput = new FileInputStream(f);
    int len;
    while ((len = fileInput.read(buffer)) > 0) {
      zipOutputStream.write(buffer, 0, len);
    }
    fileInput.close();
    zipOutputStream.closeEntry();
  }
  
  zipOutputStream.close();
}

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

@ExpectWarning("AM_CREATES_EMPTY_ZIP_FILE_ENTRY")
void bug2(ZipOutputStream any, ZipEntry anyZipEntry, int anyValue) throws IOException {
  any.putNextEntry(anyZipEntry);
  any.closeEntry();
  any.write(anyValue);
}

代码示例来源:origin: TeamNewPipe/NewPipe

/**
 * This function helps to create zip files.
 * Caution this will override the original file.
 * @param outZip The ZipOutputStream where the data should be stored in
 * @param file The path of the file that should be added to zip.
 * @param name The path of the file inside the zip.
 * @throws Exception
 */
public static void addFileToZip(ZipOutputStream outZip, String file, String name) throws Exception {
  byte data[] = new byte[BUFFER_SIZE];
  FileInputStream fi = new FileInputStream(file);
  BufferedInputStream inputStream = new BufferedInputStream(fi, BUFFER_SIZE);
  ZipEntry entry = new ZipEntry(name);
  outZip.putNextEntry(entry);
  int count;
  while((count = inputStream.read(data, 0, BUFFER_SIZE)) != -1) {
    outZip.write(data, 0, count);
  }
  inputStream.close();
}

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

@NoWarning("AM_CREATES_EMPTY_ZIP_FILE_ENTRY")
void notBug(ZipOutputStream any, ZipEntry anyZipEntry, byte[] anyBytes, int anyOffset, int anySize) throws IOException {
  any.putNextEntry(anyZipEntry);
  any.write(anyBytes, anyOffset, anySize);
  any.closeEntry();
}

相关文章