use of java.util.zip.GZIPOutputStream in project spring-framework by spring-projects.
the class GzipResourceResolverTests method createGzFile.
private static void createGzFile(String filePath) throws IOException {
Resource location = new ClassPathResource("test/", GzipResourceResolverTests.class);
Resource fileResource = new FileSystemResource(location.createRelative(filePath).getFile());
Path gzFilePath = Paths.get(fileResource.getFile().getAbsolutePath() + ".gz");
Files.deleteIfExists(gzFilePath);
File gzFile = Files.createFile(gzFilePath).toFile();
GZIPOutputStream out = new GZIPOutputStream(new FileOutputStream(gzFile));
FileCopyUtils.copy(fileResource.getInputStream(), out);
gzFile.deleteOnExit();
}
use of java.util.zip.GZIPOutputStream in project camel by apache.
the class GZIPHelper method compressGzip.
public static InputStream compressGzip(String contentEncoding, InputStream in) throws IOException {
if (isGzip(contentEncoding)) {
ByteArrayOutputStream os = new ByteArrayOutputStream();
GZIPOutputStream gzip = new GZIPOutputStream(os);
try {
IOHelper.copy(in, gzip);
gzip.finish();
return new ByteArrayInputStream(os.toByteArray());
} finally {
IOHelper.close(gzip, "gzip");
IOHelper.close(os, "byte array output stream");
}
} else {
return in;
}
}
use of java.util.zip.GZIPOutputStream in project camel by apache.
the class GZIPHelper method compressGZIP.
public static byte[] compressGZIP(byte[] data) throws IOException {
ByteArrayOutputStream os = new ByteArrayOutputStream();
GZIPOutputStream gzip = new GZIPOutputStream(os);
try {
gzip.write(data);
gzip.finish();
return os.toByteArray();
} finally {
IOHelper.close(gzip, "gzip");
IOHelper.close(os, "byte array output stream");
}
}
use of java.util.zip.GZIPOutputStream in project hutool by looly.
the class ZipUtil method gzip.
/**
* Gzip压缩文件
*
* @param file 被压缩的文件
* @return 压缩后的字节流
* @throws UtilException IO异常
*/
public static byte[] gzip(File file) throws UtilException {
ByteArrayOutputStream bos = new ByteArrayOutputStream((int) file.length());
GZIPOutputStream gos = null;
BufferedInputStream in;
try {
gos = new GZIPOutputStream(bos);
in = FileUtil.getInputStream(file);
IoUtil.copy(in, gos);
return bos.toByteArray();
} catch (IOException e) {
throw new UtilException(e);
} finally {
IoUtil.close(gos);
}
}
use of java.util.zip.GZIPOutputStream in project frostwire by frostwire.
the class JdkHttpClient method post.
@Override
public String post(String url, int timeout, String userAgent, String content, String postContentType, boolean gzip) throws IOException {
String result = null;
canceled = false;
final URL u = new URL(url);
final HttpURLConnection conn = (HttpURLConnection) u.openConnection();
conn.setDoOutput(true);
conn.setConnectTimeout(timeout);
conn.setReadTimeout(timeout);
conn.setRequestProperty("User-Agent", userAgent);
conn.setInstanceFollowRedirects(false);
if (conn instanceof HttpsURLConnection) {
setHostnameVerifier((HttpsURLConnection) conn);
}
byte[] data = content.getBytes("UTF-8");
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", postContentType);
conn.setRequestProperty("charset", "utf-8");
conn.setUseCaches(false);
ByteArrayInputStream in = new ByteArrayInputStream(data);
try {
OutputStream out;
if (gzip) {
out = new GZIPOutputStream(conn.getOutputStream());
} else {
out = conn.getOutputStream();
}
byte[] b = new byte[4096];
int n;
while (!canceled && (n = in.read(b, 0, b.length)) != -1) {
if (!canceled) {
out.write(b, 0, n);
out.flush();
onData(b, 0, n);
}
}
closeQuietly(out);
conn.connect();
int httpResponseCode = getResponseCode(conn);
if (httpResponseCode != HttpURLConnection.HTTP_OK && httpResponseCode != HttpURLConnection.HTTP_PARTIAL) {
throw new ResponseCodeNotSupportedException(httpResponseCode);
}
if (canceled) {
onCancel();
} else {
BufferedInputStream bis = new BufferedInputStream(conn.getInputStream(), 4096);
ByteArrayBuffer baf = new ByteArrayBuffer(1024);
byte[] buffer = new byte[64];
int read;
while (true) {
read = bis.read(buffer);
if (read == -1) {
break;
}
baf.append(buffer, 0, read);
}
result = new String(baf.toByteArray());
onComplete();
}
} catch (Exception e) {
onError(e);
} finally {
closeQuietly(in);
closeQuietly(conn);
}
return result;
}
Aggregations