java.util.zip.GZIPOutputStream类的使用及代码示例

x33g5p2x  于2022-01-19 转载在 其他  
字(8.7k)|赞(0)|评价(0)|浏览(172)

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

GZIPOutputStream介绍

[英]The GZIPOutputStream class is used to write data to a stream in the GZIP storage format.

Example

Using GZIPOutputStream is a little easier than ZipOutputStreambecause GZIP is only for compression, and is not a container for multiple files. This code creates a GZIP stream, similar to the gzip(1) utility.

OutputStream os = ... 
byte[] bytes = ... 
GZIPOutputStream zos = new GZIPOutputStream(new BufferedOutputStream(os)); 
try { 
zos.write(bytes); 
} finally { 
zos.close(); 
}

[中]GZIPOutputStream类用于将数据写入GZIP存储格式的流。
####范例
使用GZIPOutputStream比使用ZipOutputStream简单一些,因为GZIP只用于压缩,而不是多个文件的容器。这段代码创建了一个GZIP流,类似于GZIP(1)实用程序。

OutputStream os = ... 
byte[] bytes = ... 
GZIPOutputStream zos = new GZIPOutputStream(new BufferedOutputStream(os)); 
try { 
zos.write(bytes); 
} finally { 
zos.close(); 
}

代码示例

代码示例来源:origin: weibocom/motan

public byte[] compress(byte[] org, boolean useGzip, int minGzSize) throws IOException {
  if (useGzip && org.length > minGzSize) {
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    GZIPOutputStream gos = new GZIPOutputStream(outputStream);
    gos.write(org);
    gos.finish();
    gos.flush();
    gos.close();
    byte[] ret = outputStream.toByteArray();
    return ret;
  } else {
    return org;
  }
}

代码示例来源:origin: Netflix/zuul

public void finish() throws RuntimeException {
  try {
    gzos.finish();
    gzos.flush();
    gzos.close();
  }
  catch (IOException ioEx) {
    throw new ZuulException(ioEx, "Error finalizing the GzipOutputStream", true);
  }
}

代码示例来源:origin: lets-blade/blade

public static void compressGZIP(File input, File output) throws IOException {
  try (GZIPOutputStream out = new GZIPOutputStream(new FileOutputStream(output))) {
    try (FileInputStream in = new FileInputStream(input)) {
      byte[] buffer = new byte[1024];
      int    len;
      while ((len = in.read(buffer)) != -1) {
        out.write(buffer, 0, len);
      }
    }
  }
}

代码示例来源:origin: stanfordnlp/CoreNLP

public byte[] convertToBytes(List<Tree> input) {
 try {
  ByteArrayOutputStream bos = new ByteArrayOutputStream();
  GZIPOutputStream gos = new GZIPOutputStream(bos);
  ObjectOutputStream oos = new ObjectOutputStream(gos);
  List<Tree> transformed = CollectionUtils.transformAsList(input, treeBasicCategories);
  List<Tree> filtered = CollectionUtils.filterAsList(transformed, treeFilter);
  oos.writeObject(filtered.size());
  for (Tree tree : filtered) {
   oos.writeObject(tree.toString());
  }
  oos.close();
  gos.close();
  bos.close();
  return bos.toByteArray();
 } catch (IOException e) {
  throw new RuntimeIOException(e);
 }
}

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

@Override
public byte[] serialize(Object object) {
  try {
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    GZIPOutputStream gos = new GZIPOutputStream(bos);
    ObjectOutputStream oos = new ObjectOutputStream(gos);
    oos.writeObject(object);
    oos.close();
    return bos.toByteArray();
  } catch (IOException e) {
    throw new RuntimeException(e);
  }
}

代码示例来源:origin: FudanNLP/fnlp

public static void saveObject(String path, Object obj) throws IOException {
  ObjectOutputStream out = new ObjectOutputStream(
      new BufferedOutputStream(new GZIPOutputStream(
          new FileOutputStream(path))));
  out.writeObject(obj);
  out.close();
}
/**

代码示例来源:origin: thinkaurelius/titan

@Override
public byte[] compress(String text) {
  ByteArrayOutputStream baos = new ByteArrayOutputStream();
  try {
    OutputStream out = new GZIPOutputStream(baos);
    out.write(text.getBytes("UTF-8"));
    out.close();
  } catch (IOException e) {
    throw new RuntimeException(e);
  }
  return baos.toByteArray();
}

代码示例来源:origin: stanfordnlp/CoreNLP

/**
 * Writes a string to a temporary file.
 *
 * @param contents The string to write
 * @param path The file path
 * @param encoding The encoding to encode in
 * @throws IOException In case of failure
 * @return The File written to
 */
public static File writeStringToTempFile(String contents, String path, String encoding) throws IOException {
 OutputStream writer;
 File tmp = File.createTempFile(path,".tmp");
 if (path.endsWith(".gz")) {
  writer = new GZIPOutputStream(new FileOutputStream(tmp));
 } else {
  writer = new BufferedOutputStream(new FileOutputStream(tmp));
 }
 writer.write(contents.getBytes(encoding));
 writer.close();
 return tmp;
}

代码示例来源:origin: loklak/loklak_server

public static void gzip(File source, File dest, boolean deleteSource) throws IOException {
  byte[] buffer = new byte[2^20];
  GZIPOutputStream out = new GZIPOutputStream(new BufferedOutputStream(new FileOutputStream(dest), 65536)){{def.setLevel(Deflater.BEST_COMPRESSION);}};
  FileInputStream in = new FileInputStream(source);
  int l; while ((l = in.read(buffer)) > 0) out.write(buffer, 0, l);
  in.close(); out.finish(); out.close();
  if (deleteSource && dest.exists()) source.delete();
}

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

/**
 * Compresses a file into gzip archive.
 */
public static File gzip(File file) throws IOException {
  if (file.isDirectory()) {
    throw new IOException("Can't gzip folder");
  }
  FileInputStream fis = new FileInputStream(file);
  String gzipName = file.getAbsolutePath() + GZIP_EXT;
  GZIPOutputStream gzos = new GZIPOutputStream(new FileOutputStream(gzipName));
  try {
    StreamUtil.copy(fis, gzos);
  } finally {
    StreamUtil.close(gzos);
    StreamUtil.close(fis);
  }
  return new File(gzipName);
}

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

// DON'T DO THIS
try (BufferedWriter bw = new BufferedWriter(
    new OutputStreamWriter(
    new GZIPOutputStream(
    new FileOutputStream(createdFile))))) {
  // ...
}

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

private void gzipFile(String srcPath, String destPath) throws Exception {
  byte[] buffer = new byte[1024];
  FileOutputStream fileOutputStream = new FileOutputStream(destPath);
  DataOutputStream out = new DataOutputStream(new BufferedOutputStream(new GZIPOutputStream(fileOutputStream)));
  FileInputStream fileInput = new FileInputStream(srcPath);
  int bytes_read;
  while((bytes_read = fileInput.read(buffer)) > 0) {
    out.write(buffer, 0, bytes_read);
  }
  fileInput.close();
  out.close();
}

代码示例来源:origin: cymcsg/UltimateAndroid

/**
 * Gzip file.
 *
 * @param zipFileName
 * @param mDestFile
 * @throws Exception
 */
public static void gzips(String zipFileName, String mDestFile) throws Exception {
  BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(zipFileName), "UTF-8"));
  BufferedOutputStream out = new BufferedOutputStream(new GZIPOutputStream(new FileOutputStream(mDestFile)));
  int c;
  while ((c = in.read()) != -1) {
    out.write(String.valueOf((char) c).getBytes("UTF-8"));
  }
  in.close();
  out.close();
}

代码示例来源:origin: soabase/exhibitor

void        compress() throws IOException
{
  byte[]          buffer = new byte[BUFFER_SIZE];
  InputStream     in = null;
  OutputStream    out = null;
  try
  {
    in = new FileInputStream(source);
    out = new GZIPOutputStream(new FileOutputStream(tempFile));
    for(;;)
    {
      int     bytesRead = in.read(buffer);
      if ( bytesRead < 0 )
      {
        break;
      }
      out.write(buffer, 0, bytesRead);
    }
  }
  finally
  {
    CloseableUtils.closeQuietly(in);
    CloseableUtils.closeQuietly(out);
  }
}

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

public static byte[] toCompressedJsonConf(Map<String, Object> topoConf) {
  try {
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    OutputStreamWriter out = new OutputStreamWriter(new GZIPOutputStream(bos));
    JSONValue.writeJSONString(topoConf, out);
    out.close();
    return bos.toByteArray();
  } catch (IOException e) {
    throw new RuntimeException(e);
  }
}

代码示例来源:origin: Sable/soot

streamOut = jarFile;
 } else {
  streamOut = new FileOutputStream(fileName);
  streamOut = new GZIPOutputStream(streamOut);
 writerOut = new PrintWriter(new OutputStreamWriter(streamOut));
} catch (IOException e) {
 throw new CompilationDeathException("Cannot output file " + fileName, e);
  writerOut.flush();
  if (jarFile == null) {
   streamOut.close();
 OutputStream streamOut = new FileOutputStream(fileName);
 PrintWriter writerOut = new PrintWriter(new OutputStreamWriter(streamOut));
 DavaBuildFile.generate(writerOut, decompiledClasses);
 writerOut.flush();
 streamOut.close();
} catch (IOException e) {
 throw new CompilationDeathException("Cannot output file " + fileName, e);

代码示例来源:origin: Netflix/eureka

private static byte[] toGzippedJson(InstanceInfo remoteInfo) throws IOException {
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    GZIPOutputStream gos = new GZIPOutputStream(bos);
    EurekaJacksonCodec.getInstance().writeTo(remoteInfo, gos);
    gos.flush();
    return bos.toByteArray();
  }
}

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

temp.deleteOnExit();
  System.out.println("Generating test data in " + temp);
  OutputStream outputStream = new GZIPOutputStream(new FileOutputStream(temp));
  Writer writer = new BufferedWriter(new OutputStreamWriter(outputStream),
                    10 * 1024 * 1024);
  String value = TestUtils.randomLetters(valueSize);
InputStream inputStream = new GZIPInputStream(new FileInputStream(temp));
Reader r = new BufferedReader(new InputStreamReader(inputStream), 1 * 1024 * 1024);
File output = TestUtils.createTempDir(workingDir);

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

FileOutputStream output = new FileOutputStream(fileName);
try {
 Writer writer = new OutputStreamWriter(new GZIPOutputStream(output), "UTF-8"));
 try {
  writer.write(text);
 } finally {
  writer.close();
 }
 } finally {
  output.close();
 }

代码示例来源:origin: stanfordnlp/CoreNLP

private static OutputStream getBufferedOutputStream(String path) throws IOException {
 OutputStream os = new BufferedOutputStream(new FileOutputStream(path));
 if (path.endsWith(".gz")) {
  os = new GZIPOutputStream(os);
 }
 return os;
}

相关文章