UnGzip和Untar inputStream

时间:2014-03-27 17:07:38

标签: java

我试图在java中unGzip和unTar一个inputStream,我有这些方法:

public InputStream unTar(InputStream in) throws IOException {

    TarInputStream myTarStream = new TarInputStream(in);
    TarEntry entry = myTarStream.getNextEntry();
    InputStream input = null;
    while (entry != null) {
        ByteArrayOutputStream output = new ByteArrayOutputStream();
        byte[] buff = new byte[1024];
        int read;
        do {
            read = myTarStream.read(buff);
            if (read != -1) {
                output.write(buff, 0, read);
            }
        } while (read != -1);
        output.flush();
        input = new ByteArrayInputStream(output.toByteArray());
        entry = myTarStream.getNextEntry();
    }
    myTarStream.close();

    return input;
}

public InputStream unGzipIt(InputStream in) {

    byte[] buffer = new byte[1024];
    InputStream outGZIPStream = null;
    try {
        ByteArrayOutputStream bytesOutput = new ByteArrayOutputStream();
        GZIPInputStream gis = new GZIPInputStream(in);
        int len;
        while ((len = gis.read(buffer)) > 0) {
            bytesOutput.write(buffer, 0, len);
        }
        in.close();
        bytesOutput.close();
        outGZIPStream = new ByteArrayInputStream(bytesOutput.toByteArray());

    } catch (IOException e) {
        e.printStackTrace();
    }

    return outGZIPStream;
}

问题是当我从本地的文件传递一个inputStream时,它工作, 但是当我从服务器响应中传递inputStream时,它不起作用。

我应该使用重置和标记吗?任何帮助?谢谢

这就是我如何获得inputStream:

 public InputStream getFolder(@PathParam("id") String envId, @PathParam("appName") String appName, @PathParam("imageType") String imageType,
                                 @QueryParam("folderPath") String folderPath) throws EnvAutomationException, IOException {
        Environment env = Envs.getEnvironmentManager().findEnvironment(envId);
        ApplicationInstance appInst = env.getApplicationInstance(appName);
        Container container = appInst.getContainer(imageType);
        InputStream folderData = Envs.getContainerizationManager().getFolder(container, folderPath);
        ByteArrayOutputStream output = new ByteArrayOutputStream();
        byte[] buff = new byte[1024];
        int size = 0;
        int read;
        do {
            read = folderData.read(buff);
            if (read != -1) {
                size = size + read;
                output.write(buff, 0, read);
            }
        } while (read != -1);
        output.flush();
        byte[] bo = output.toByteArray();
        InputStream input = new ByteArrayInputStream(bo);
        InputStream inputGZIP = gzipIt(input);
        return inputGZIP;
    }

因为它是一个.tar文件,我发誓它,这是gzip的方法;  public InputStream gzipIt(InputStream source){

    byte[] buffer = new byte[1024];
    InputStream outGZIPStream = null;
    try {
        ByteArrayOutputStream bytesOutput = new ByteArrayOutputStream();
        GZIPOutputStream gzos = new GZIPOutputStream(bytesOutput);
        int len;
        int size = 0;
        while ((len = source.read(buffer)) > 0) {
            gzos.write(buffer, 0, len);
            size = size + len;
        }
        source.close();
        gzos.close();
        byte[] bo = bytesOutput.toByteArray();
        outGZIPStream = new ByteArrayInputStream(bytesOutput.toByteArray());
        logger.info("folder tar size :" + size +  " ; folder gzip size  " + bo.length);
    } catch (IOException e) {
        e.printStackTrace();
    }

    return outGZIPStream;
}

0 个答案:

没有答案