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

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

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

GZIPOutputStream.write介绍

[英]Write up to nbytes of data from the given buffer, starting at offset off, to the underlying stream in GZIP format.
[中]从offset off开始,以GZIP格式从给定缓冲区向底层流写入多达N字节的数据。

代码示例

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

public static byte[] gzip(byte[] data) {
  try {
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    GZIPOutputStream out = new GZIPOutputStream(bos);
    out.write(data);
    out.close();
    return bos.toByteArray();
  } catch (IOException e) {
    throw new RuntimeException(e);
  }
}

代码示例来源:origin: oblac/jodd

/**
 * Writes byte array to gzip output stream. Creates new <code>GZIPOutputStream</code>
 * if not created yet. Also sets the "Content-Encoding" header.
 */
public void writeToGZip(final byte[] b, final int off, final int len) throws IOException {
  if (gzipstream == null) {
    gzipstream = new GZIPOutputStream(output);
    response.setHeader("Content-Encoding", "gzip");
  }
  gzipstream.write(b, off, len);
}

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

public static String encodeBase64ZippedString( String in ) throws IOException {
  Charset charset = Charset.forName( Const.XML_ENCODING );
  ByteArrayOutputStream baos = new ByteArrayOutputStream( 1024 );
  try ( Base64OutputStream base64OutputStream = new Base64OutputStream( baos );
     GZIPOutputStream gzos = new GZIPOutputStream( base64OutputStream ) ) {
   gzos.write( in.getBytes( charset ) );
  }
  return baos.toString();
 }
}

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

private void flushToGZIP() throws IOException {
  if (stream instanceof ByteArrayOutputStream) {
    // indication de compression,
    // on utilise setHeader et non addHeader pour être compatible avec PJL compression filter
    // en particulier dans le plugin grails vis à vis de l'autre plugin grails UiPerformance
    response.setHeader("Content-Encoding", "gzip");
    response.setHeader("Vary", "Accept-Encoding");
    // make new gzip stream using the response output stream (content-encoding is in constructor)
    final GZIPOutputStream gzipstream = new GZIPOutputStream(response.getOutputStream(),
        compressionThreshold);
    // get existing bytes
    final byte[] bytes = ((ByteArrayOutputStream) stream).toByteArray();
    gzipstream.write(bytes);
    // we are no longer buffering, send content via gzipstream
    stream = gzipstream;
  }
}

代码示例来源:origin: cSploit/android

public static String saveSession(String sessionName) throws IOException {
 StringBuilder builder = new StringBuilder();
 String filename = mStoragePath + '/' + sessionName + ".dss",
     session;
 builder.append(SESSION_MAGIC + "\n");
 // skip the network target
 synchronized (mTargets) {
  builder.append(mTargets.size() - 1).append("\n");
  for (Target target : mTargets) {
   if (target.getType() != Target.Type.NETWORK)
    target.serialize(builder);
  }
 }
 session = builder.toString();
 FileOutputStream ostream = new FileOutputStream(filename);
 GZIPOutputStream gzip = new GZIPOutputStream(ostream);
 gzip.write(session.getBytes());
 gzip.close();
 mSessionName = sessionName;
 return filename;
}

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

private static byte[] compressIfNecessary(byte[] dataBytes) throws IOException {
  // enable compression when data is larger than 1KB
  int maxDataSizeUncompress = 1024;
  if (dataBytes.length < maxDataSizeUncompress) {
    return dataBytes;
  }
  ByteArrayOutputStream out = new ByteArrayOutputStream();
  GZIPOutputStream gzip = new GZIPOutputStream(out);
  gzip.write(dataBytes);
  gzip.close();
  return out.toByteArray();
}

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

/**
  * Base64 encodes then zips a byte array into a compressed string
  *
  * @param src the source byte array
  * @return a compressed, base64 encoded string
  * @throws IOException
  */
 public static String encodeBase64Zipped( byte[] src ) throws IOException {
  ByteArrayOutputStream baos = new ByteArrayOutputStream( 1024 );
  try ( Base64OutputStream base64OutputStream = new Base64OutputStream( baos );
     GZIPOutputStream gzos = new GZIPOutputStream( base64OutputStream ) ) {
   gzos.write( src );
  }
  return baos.toString();
 }
}

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

/**
 Compresses the given file and removes the uncompressed file
 @param file name of the zip file
 @return Size of the zip file
 */
private long zipFile(String file) throws IOException
{
  cleanDiskSpace();
  if (hasSpace())
  {
    String zipFile = file + ".gz";
    FileInputStream is = new FileInputStream(file);
    GZIPOutputStream gout = new GZIPOutputStream(new FileOutputStream(zipFile));
    byte[] buffer = new byte[1024];
    int readSize;
    while ((readSize = is.read(buffer)) != -1)
      gout.write(buffer, 0, readSize);
    is.close();
    gout.flush();
    gout.close();
    //delete uncompressed file
    new File(file).delete();
    return (new File(zipFile).length());
  }
  else
  {
    logger.error("No space available to create zip files after attempting clean up");
    return 0;
  }
}

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

public static byte[] gzip(byte[] data) {
  try {
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    GZIPOutputStream out = new GZIPOutputStream(bos);
    out.write(data);
    out.close();
    return bos.toByteArray();
  } catch (IOException e) {
    throw new RuntimeException(e);
  }
}

代码示例来源:origin: cSploit/android

public String save(String sessionName) throws IOException {
 StringBuilder builder = new StringBuilder();
 String filename = System.getStoragePath() + '/' + sessionName + ".dhs",
     buffer = null;
 builder.append( System.SESSION_MAGIC + "\n");
 builder.append(mUserName == null ? "null" : mUserName).append("\n");
 builder.append(mHTTPS).append("\n");
 builder.append(mAddress).append("\n");
 builder.append(mDomain).append("\n");
 builder.append(mUserAgent).append("\n");
 builder.append(mCookies.size()).append("\n");
 for(HttpCookie cookie : mCookies.values()){
  builder
   .append(cookie.getName()).append( "=" ).append(cookie.getValue())
   .append( "; domain=").append(cookie.getDomain())
   .append( "; path=/" )
   .append( mHTTPS ? ";secure" : "" )
   .append("\n");
 }
 buffer = builder.toString();
 FileOutputStream ostream = new FileOutputStream(filename);
 GZIPOutputStream gzip = new GZIPOutputStream(ostream);
 gzip.write(buffer.getBytes());
 gzip.close();
 return filename;
}

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

public static byte[] compressGZIPAsString(String content, Charset charset) throws IOException {
  if (content == null || content.length() == 0) {
    return null;
  }
  GZIPOutputStream      gzip;
  ByteArrayOutputStream out = new ByteArrayOutputStream();
  gzip = new GZIPOutputStream(out);
  gzip.write(content.getBytes(charset));
  gzip.close();
  return out.toByteArray();
}

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

/**
 *
 * @param fileWorkArea
 * @param fileNames
 */
protected void gzipAndDeleteFiles(FileWorkArea fileWorkArea, List<String> fileNames,boolean shouldDeleteOriginal){
  for (String fileName : fileNames) {
    try {
      String fileNameWithPath = FilenameUtils.normalize(fileWorkArea.getFilePathLocation() + File.separator + fileName);
      FileInputStream fis = new FileInputStream(fileNameWithPath);
      FileOutputStream fos = new FileOutputStream(fileNameWithPath + ENCODING_EXTENSION);
      GZIPOutputStream gzipOS = new GZIPOutputStream(fos);
      byte[] buffer = new byte[1024];
      int len;
      while ((len = fis.read(buffer)) != -1) {
        gzipOS.write(buffer, 0, len);
      }
      //close resources
      gzipOS.close();
      fos.close();
      fis.close();
      if(shouldDeleteOriginal){
        File originalFile = new File(fileNameWithPath);
        originalFile.delete();
      }
    } catch (IOException e) {
      LOG.error("Error writing zip file.", e);
    }
  }
}

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

/**
 * Similar to the unix gzip command, only it does not delete the file after compressing it.
 * 
 * @param uncompressedFileName The file to gzip
 * @param compressedFileName The file name for the compressed file
 * @throws IOException
 */
public static void gzipFile(File uncompressedFileName, File compressedFileName) throws IOException {
 try (GZIPOutputStream out = new GZIPOutputStream(new FileOutputStream(compressedFileName));
    FileInputStream in = new FileInputStream(uncompressedFileName)) {
  byte[] buf = new byte[1024];
  for (int len; (len = in.read(buf)) > 0; ) {
   out.write(buf, 0, len);
  }
 }
}

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

public static byte[] compressGZIPAsString(String content, Charset charset) throws IOException {
  if (content == null || content.length() == 0) {
    return null;
  }
  GZIPOutputStream      gzip;
  ByteArrayOutputStream out = new ByteArrayOutputStream();
  gzip = new GZIPOutputStream(out);
  gzip.write(content.getBytes(charset));
  gzip.close();
  return out.toByteArray();
}

代码示例来源:origin: SonarSource/sonarqube

GZIPOutputStream gzipOutputStream = new GZIPOutputStream(resp.getOutputStream());
 gzipOutputStream.write("GZIP response".getBytes());
 gzipOutputStream.close();
} else {
 resp.getPrintStream().append("agent=" + req.getValues("User-Agent").get(0));

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

private void outputSampleCsv(final Path csvSamplePath) throws IOException {
  // TODO(dmikurube): Move the data into Java resources.
  StringBuilder csvBuilder = new StringBuilder();
  csvBuilder.append("id,account,time,purchase,comment\n");
  csvBuilder.append("1,32864,2015-01-27 19:23:49,20150127,embulk\n");
  csvBuilder.append("2,14824,2015-01-27 19:01:23,20150127,embulk jruby\n");
  csvBuilder.append("3,27559,2015-01-28 02:20:02,20150128,\"Embulk \"\"csv\"\" parser plugin\"\n");
  csvBuilder.append("4,11270,2015-01-29 11:54:36,20150129,NULL\n");
  csvBuilder.append("\n");
  byte[] csvSample = csvBuilder.toString().getBytes(StandardCharsets.UTF_8);
  try (GZIPOutputStream output = new GZIPOutputStream(Files.newOutputStream(csvSamplePath))) {
    output.write(csvSample);
  }
}

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

public static byte[] gzip(byte[] data) throws IOException {
  ByteArrayOutputStream bos = new ByteArrayOutputStream(data.length);
  GZIPOutputStream gzip = null;
  try {
    gzip = new GZIPOutputStream(bos);
    gzip.write(data);
    gzip.finish();
    return bos.toByteArray();
  } finally {
    if (gzip != null) {
      gzip.close();
    }
  }
}

相关文章