Java Zip File zip(InputStream is)

Here you can find the source of zip(InputStream is)

Description

zip

License

Open Source License

Declaration

public static byte[] zip(InputStream is) throws IOException 

Method Source Code

//package com.java2s;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;

import java.io.ByteArrayOutputStream;

import java.io.IOException;
import java.io.InputStream;

import java.io.OutputStream;

import java.util.LinkedList;
import java.util.List;

import java.util.zip.ZipEntry;

import java.util.zip.ZipOutputStream;

public class Main {
    public static byte[] zip(InputStream is) throws IOException {
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        ZipOutputStream zos = new ZipOutputStream(bos);
        ZipEntry ze = new ZipEntry("X");
        zos.putNextEntry(ze);/*from w  ww .  jav  a2 s  .co m*/
        pipe(is, zos);
        zos.closeEntry();
        zos.flush();
        zos.close();
        return bos.toByteArray();
    }

    /**
     * Reads bytes from the <tt>from</tt> input stream and writes them to the
     * <tt>to</tt> output stream.
     * 
     * @param from
     * @param to
     * @throws IOException
     */
    public static void pipe(InputStream from, OutputStream to) throws IOException {
        byte[] buffer = new byte[1024];
        BufferedInputStream bis = new BufferedInputStream(from);
        BufferedOutputStream bos = new BufferedOutputStream(to);
        int avail;
        while ((avail = from.available()) > 0) {
            for (int off = 0; off < avail; off += 1024) {
                int length = Math.min(avail - off, 1024);
                bis.read(buffer, 0, length);
                bos.write(buffer, 0, length);
            }
        }
        bos.flush();
    }

    public static byte[] toByteArray(String hexString) {
        byte[] bytes = new byte[hexString.length() / 2];
        for (int i = 0; i < hexString.length(); i += 2) {
            String hex = hexString.substring(i, i + 2);
            bytes[i / 2] = (byte) Integer.parseInt(hex, 16);
        }
        return bytes;
    }

    /**
     * Converts (a portion) of the given stream to a byte array.
     * 
     * @param inputStream
     *            the stream from which to read bytes
     * @param size
     *            the number of bytes to be read, or -1 if the whole of the
     *            (remaining) bytes should be read
     * @return a byte array of length <tt>size</tt> containing bytes read from
     *         the given stream, starting at the current position
     * @throws IOException
     */
    public static byte[] toByteArray(InputStream inputStream, final int size) throws IOException {
        int totalSize = 0;
        List<byte[]> bufferList = null;
        byte[] result = null;
        if (size == -1) {
            bufferList = new LinkedList<byte[]>();
        } else {
            result = new byte[size];
        }
        int avail = inputStream.available();
        while (avail > 0) {
            byte[] buffer = new byte[1024];
            int len;
            if (size == -1) {
                len = 1024;
            } else {
                len = Math.min(1024, size - totalSize);
            }
            int r = inputStream.read(buffer, 0, len);
            if (r == -1) {
                break;
            }
            if (size == -1) {
                if (r < 1024) {
                    byte[] smallerBuffer = new byte[r];
                    System.arraycopy(buffer, 0, smallerBuffer, 0, r);
                    bufferList.add(smallerBuffer);
                } else {
                    bufferList.add(buffer);
                }
            } else {
                System.arraycopy(buffer, 0, result, totalSize, r);
            }
            totalSize += r;
            if (size != -1 && totalSize == size) {
                break;
            }
        }
        if (size == -1) {
            result = new byte[totalSize];
            int offset = 0;
            for (byte[] buffer : bufferList) {
                System.arraycopy(buffer, 0, result, offset, buffer.length);
                offset += buffer.length;
            }
        }
        return result;
    }

    public static String substring(String string, int pos, int length) {
        if (string.length() - pos <= length) {
            return string.substring(pos);
        }
        return string.substring(pos, pos + length);
    }
}

Related

  1. zip(final File zipFile, final File[] files)
  2. zip(final File[] filesToZip, final File zipFile)
  3. zip(final Map files)
  4. zip(final String fileName, final byte[] fileContent)
  5. zip(final String sourceFileDir, final String zipFile)
  6. zip(InputStream is, String dataFileName, File zipFile)
  7. zip(OutputStream outputStream, File targetFile)
  8. zip(String data, String fileName)
  9. zip(String dir, String destFile)