Search in sources :

Example 51 with GZIPInputStream

use of java.util.zip.GZIPInputStream in project zipkin by openzipkin.

the class ZipkinHttpCollector method gunzip.

static byte[] gunzip(byte[] input) throws IOException {
    GZIPInputStream in = new GZIPInputStream(new ByteArrayInputStream(input));
    try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream(input.length)) {
        byte[] buf = GZIP_BUFFER.get();
        int len;
        while ((len = in.read(buf)) > 0) {
            outputStream.write(buf, 0, len);
        }
        return outputStream.toByteArray();
    }
}
Also used : GZIPInputStream(java.util.zip.GZIPInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 52 with GZIPInputStream

use of java.util.zip.GZIPInputStream in project jodd by oblac.

the class ZipUtil method ungzip.

/**
	 * Decompress gzip archive.
	 */
public static File ungzip(File file) throws IOException {
    String outFileName = FileNameUtil.removeExtension(file.getAbsolutePath());
    File out = new File(outFileName);
    out.createNewFile();
    FileOutputStream fos = new FileOutputStream(out);
    GZIPInputStream gzis = new GZIPInputStream(new FileInputStream(file));
    try {
        StreamUtil.copy(gzis, fos);
    } finally {
        StreamUtil.close(fos);
        StreamUtil.close(gzis);
    }
    return out;
}
Also used : GZIPInputStream(java.util.zip.GZIPInputStream) FileOutputStream(java.io.FileOutputStream) File(java.io.File) ZipFile(java.util.zip.ZipFile) FileInputStream(java.io.FileInputStream)

Example 53 with GZIPInputStream

use of java.util.zip.GZIPInputStream in project nutz by nutzam.

the class Sender method createResponse.

protected Response createResponse(Map<String, String> reHeaders) throws IOException {
    Response rep = null;
    if (reHeaders != null) {
        rep = new Response(conn, reHeaders);
        if (rep.isOK()) {
            InputStream is1 = conn.getInputStream();
            InputStream is2 = null;
            String encoding = conn.getContentEncoding();
            // 如果采用了压缩,则需要处理否则都是乱码
            if (encoding != null && encoding.contains("gzip")) {
                is2 = new GZIPInputStream(is1);
            } else if (encoding != null && encoding.contains("deflate")) {
                is2 = new InflaterInputStream(is1, new Inflater(true));
            } else {
                is2 = is1;
            }
            BufferedInputStream is = new BufferedInputStream(is2);
            rep.setStream(is);
        } else {
            try {
                rep.setStream(conn.getInputStream());
            } catch (IOException e) {
                try {
                    rep.setStream(conn.getErrorStream());
                } catch (Exception e1) {
                    rep.setStream(new VoidInputStream());
                }
            }
        }
    }
    if (this.interceptor != null)
        this.interceptor.afterResponse(request, conn, rep);
    return rep;
}
Also used : GZIPInputStream(java.util.zip.GZIPInputStream) BufferedInputStream(java.io.BufferedInputStream) GZIPInputStream(java.util.zip.GZIPInputStream) BufferedInputStream(java.io.BufferedInputStream) InflaterInputStream(java.util.zip.InflaterInputStream) VoidInputStream(org.nutz.lang.stream.VoidInputStream) InputStream(java.io.InputStream) InflaterInputStream(java.util.zip.InflaterInputStream) VoidInputStream(org.nutz.lang.stream.VoidInputStream) Inflater(java.util.zip.Inflater) IOException(java.io.IOException) IOException(java.io.IOException)

Example 54 with GZIPInputStream

use of java.util.zip.GZIPInputStream in project jodd by oblac.

the class HttpResponse method unzip.

// ---------------------------------------------------------------- body
/**
	 * Unzips GZip-ed body content, removes the content-encoding header
	 * and sets the new content-length value.
	 */
public HttpResponse unzip() {
    String contentEncoding = contentEncoding();
    if (contentEncoding != null && contentEncoding().equals("gzip")) {
        if (body != null) {
            removeHeader(HEADER_CONTENT_ENCODING);
            try {
                ByteArrayInputStream in = new ByteArrayInputStream(body.getBytes(StringPool.ISO_8859_1));
                GZIPInputStream gzipInputStream = new GZIPInputStream(in);
                ByteArrayOutputStream out = new ByteArrayOutputStream();
                StreamUtil.copy(gzipInputStream, out);
                body(out.toString(StringPool.ISO_8859_1));
            } catch (IOException ioex) {
                throw new HttpException(ioex);
            }
        }
    }
    return this;
}
Also used : GZIPInputStream(java.util.zip.GZIPInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException)

Example 55 with GZIPInputStream

use of java.util.zip.GZIPInputStream in project openhab1-addons by openhab.

the class IhcClient method LoadProjectFileFromController.

private Document LoadProjectFileFromController() throws IhcExecption {
    try {
        WSProjectInfo projectInfo = getProjectInfo();
        int numberOfSegments = controllerService.getProjectNumberOfSegments();
        int segmentationSize = controllerService.getProjectSegmentationSize();
        logger.debug("Number of segments: {}", numberOfSegments);
        logger.debug("Segmentation size: {}", segmentationSize);
        ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
        for (int i = 0; i < numberOfSegments; i++) {
            logger.debug("Downloading segment {}", i);
            WSFile data = controllerService.getProjectSegment(i, projectInfo.getProjectMajorRevision(), projectInfo.getProjectMinorRevision());
            byteStream.write(data.getData());
        }
        logger.debug("File size before base64 encoding: {} bytes", byteStream.size());
        byte[] decodedBytes = javax.xml.bind.DatatypeConverter.parseBase64Binary(byteStream.toString());
        logger.debug("File size after base64 encoding: {} bytes", decodedBytes.length);
        GZIPInputStream gzis = new GZIPInputStream(new ByteArrayInputStream(decodedBytes));
        InputStreamReader in = new InputStreamReader(gzis, "ISO-8859-1");
        InputSource reader = new InputSource(in);
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        return db.parse(reader);
    } catch (Exception e) {
        throw new IhcExecption(e);
    }
}
Also used : InputSource(org.xml.sax.InputSource) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) InputStreamReader(java.io.InputStreamReader) ByteArrayOutputStream(java.io.ByteArrayOutputStream) SocketTimeoutException(java.net.SocketTimeoutException) WSFile(org.openhab.binding.ihc.ws.datatypes.WSFile) GZIPInputStream(java.util.zip.GZIPInputStream) WSProjectInfo(org.openhab.binding.ihc.ws.datatypes.WSProjectInfo) ByteArrayInputStream(java.io.ByteArrayInputStream) DocumentBuilder(javax.xml.parsers.DocumentBuilder)

Aggregations

GZIPInputStream (java.util.zip.GZIPInputStream)376 InputStream (java.io.InputStream)144 IOException (java.io.IOException)125 ByteArrayInputStream (java.io.ByteArrayInputStream)120 FileInputStream (java.io.FileInputStream)98 ByteArrayOutputStream (java.io.ByteArrayOutputStream)77 InputStreamReader (java.io.InputStreamReader)57 File (java.io.File)56 BufferedReader (java.io.BufferedReader)45 BufferedInputStream (java.io.BufferedInputStream)41 Test (org.junit.Test)41 FileOutputStream (java.io.FileOutputStream)30 URL (java.net.URL)25 InflaterInputStream (java.util.zip.InflaterInputStream)25 OutputStream (java.io.OutputStream)24 GZIPOutputStream (java.util.zip.GZIPOutputStream)21 ObjectInputStream (java.io.ObjectInputStream)19 HttpURLConnection (java.net.HttpURLConnection)19 URLConnection (java.net.URLConnection)17 HashMap (java.util.HashMap)15