Search in sources :

Example 76 with GZIPOutputStream

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();
}
Also used : Path(java.nio.file.Path) GZIPOutputStream(java.util.zip.GZIPOutputStream) FileOutputStream(java.io.FileOutputStream) ClassPathResource(org.springframework.core.io.ClassPathResource) FileSystemResource(org.springframework.core.io.FileSystemResource) Resource(org.springframework.core.io.Resource) FileSystemResource(org.springframework.core.io.FileSystemResource) File(java.io.File) ClassPathResource(org.springframework.core.io.ClassPathResource)

Example 77 with GZIPOutputStream

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;
    }
}
Also used : GZIPOutputStream(java.util.zip.GZIPOutputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 78 with GZIPOutputStream

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");
    }
}
Also used : GZIPOutputStream(java.util.zip.GZIPOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 79 with GZIPOutputStream

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);
    }
}
Also used : GZIPOutputStream(java.util.zip.GZIPOutputStream) BufferedInputStream(java.io.BufferedInputStream) UtilException(cn.hutool.core.exceptions.UtilException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) FastByteArrayOutputStream(cn.hutool.core.io.FastByteArrayOutputStream) IOException(java.io.IOException)

Example 80 with GZIPOutputStream

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;
}
Also used : GZIPOutputStream(java.util.zip.GZIPOutputStream) URL(java.net.URL) HttpURLConnection(java.net.HttpURLConnection) GZIPOutputStream(java.util.zip.GZIPOutputStream) HttpsURLConnection(javax.net.ssl.HttpsURLConnection)

Aggregations

GZIPOutputStream (java.util.zip.GZIPOutputStream)835 ByteArrayOutputStream (java.io.ByteArrayOutputStream)339 IOException (java.io.IOException)254 FileOutputStream (java.io.FileOutputStream)251 OutputStream (java.io.OutputStream)185 File (java.io.File)179 Test (org.junit.Test)93 BufferedOutputStream (java.io.BufferedOutputStream)84 GZIPInputStream (java.util.zip.GZIPInputStream)77 FileInputStream (java.io.FileInputStream)72 InputStream (java.io.InputStream)64 ByteArrayInputStream (java.io.ByteArrayInputStream)60 OutputStreamWriter (java.io.OutputStreamWriter)53 ObjectOutputStream (java.io.ObjectOutputStream)39 DataOutputStream (java.io.DataOutputStream)38 BufferedWriter (java.io.BufferedWriter)30 ByteBuffer (java.nio.ByteBuffer)28 Path (java.nio.file.Path)28 ArrayList (java.util.ArrayList)28 NodeSettings (org.knime.core.node.NodeSettings)28