Here you can find the source of decompress(final int type, final InputStream is, final OutputStream os)
Parameter | Description |
---|---|
DataFormatException | an exception |
NoSuchMethodException | an exception |
public static void decompress(final int type, final InputStream is, final OutputStream os) throws IOException, NoSuchMethodException
//package com.java2s; /*/*from ww w . j a va 2s . c o m*/ * Copyright 2014 Ricardo Lorenzo<unshakablespirit@gmail.com> * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.util.zip.*; public class Main { public final static int COMPRESSION_GZIP = 1; public final static int COMPRESSION_DEFLATE = 2; public final static int COMPRESSION_ZIP = 3; /** * Decompress stream from different formats * * @throws DataFormatException * @throws NoSuchMethodException */ public static void decompress(final int type, final InputStream is, final OutputStream os) throws IOException, NoSuchMethodException { switch (type) { case COMPRESSION_GZIP: { GZIPInputStream gzipped = new GZIPInputStream(is); try { write(gzipped, os); } finally { closeQuietly(gzipped); } } case COMPRESSION_DEFLATE: { InflaterOutputStream inflated = new InflaterOutputStream(os, new Inflater(false)); try { write(is, inflated); inflated.flush(); } finally { closeQuietly(inflated); } } case COMPRESSION_ZIP: { ZipInputStream zipped = new ZipInputStream(is); try { write(zipped, os); } finally { closeQuietly(zipped); } } default: throw new NoSuchMethodException(); } } public static final void write(final InputStream is, final OutputStream os, long length) throws IOException { long offset = 0; for (; offset < length; offset++) { os.write(is.read()); } os.flush(); } public static final void write(final InputStream is, final OutputStream os) throws IOException { byte[] buffer = new byte[2048]; for (int rlenght = is.read(buffer); rlenght > 0; rlenght = is.read(buffer)) { os.write(buffer, 0, rlenght); } os.flush(); } public static final void write(byte[] data, final OutputStream os) throws IOException { os.write(data, 0, data.length); os.flush(); } public static final void write(final String content, final OutputStream os) throws IOException { os.write(content.getBytes()); } public static final void closeQuietly(final InputStream is) { try { if (is != null) { is.close(); } } catch (NullPointerException e) { // nothing } catch (IOException e) { // nothing } } public static final void closeQuietly(final OutputStream os) { try { if (os != null) { os.close(); } } catch (NullPointerException e) { // nothing } catch (IOException e) { // nothing } } public static final byte[] read(final InputStream is, final int length) throws IOException { byte[] data = new byte[length]; is.read(data, 0, data.length); return data; } }