如何将ZipInputStream转换为InputStream?

时间:2011-10-21 18:54:41

标签: java inputstream zipinputstream

我有代码,ZipInputSream转换为byte [],但我不知道如何将其转换为inputstream。

private void convertStream(String encoding, ZipInputStream in) throws IOException,
        UnsupportedEncodingException
{
    final int BUFFER = 1;
    @SuppressWarnings("unused")
    int count = 0;
    byte data[] = new byte[BUFFER];
    while ((count = in.read(data, 0, BUFFER)) != -1) 
    {
       // How can I convert data to InputStream  here ?                    
    }
}

5 个答案:

答案 0 :(得分:8)

答案 1 :(得分:2)

以下是我解决这个问题的方法。现在我可以将ZipInputStream中的单个文件作为InputStream从内存中获取。

private InputStream convertZipInputStreamToInputStream(ZipInputStream in, ZipEntry entry, String encoding) throws IOException
{
    final int BUFFER = 2048;
    int count = 0;
    byte data[] = new byte[BUFFER];
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    while ((count = in.read(data, 0, BUFFER)) != -1) {
        out.write(data);
    }       
    InputStream is = new ByteArrayInputStream(out.toByteArray());
    return is;
}

答案 2 :(得分:0)

请在下面找到将从ZIP存档中提取所有文件的功能示例。此功能不适用于子文件夹中的文件:

private static void testZip() {
    ZipInputStream zipStream = null;
    byte buff[] = new byte[16384];
    int readBytes;
    try {
        FileInputStream fis = new FileInputStream("./test.zip");
        zipStream = new ZipInputStream(fis);
        ZipEntry ze;
        while((ze = zipStream.getNextEntry()) != null) {
            if(ze.isDirectory()) {
                System.out.println("Folder : "+ze.getName());
                continue;//no need to extract
            }
            System.out.println("Extracting file "+ze.getName());
            //at this moment zipStream pointing to the beginning of current ZipEntry, e.g. archived file
            //saving file
            FileOutputStream outFile = new FileOutputStream(ze.getName());
            while((readBytes = zipStream.read(buff)) != -1) {
                outFile.write(buff, 0, readBytes);
            }
            outFile.close();                
        }

    } catch (Exception e) {
        System.err.println("Error processing zip file : "+e.getMessage());
    } finally {
        if(zipStream != null)
            try {
                zipStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
    }
}

答案 3 :(得分:-1)

ZipInputStream允许直接读取ZIP内容:使用getNextEntry()进行迭代,直到找到要阅读的条目,然后只需阅读ZipInputStream

如果您不想只读取ZIP内容,但需要在传递到下一步之前对流应用其他转换,则可以使用PipedInputStreamPipedOutputStream。这个想法与此类似(从内存中编写,甚至可能无法编译):

import java.io.PipedInputStream;
import java.io.PipedOutputStream;

public abstract class FilterThread extends Thread {
    private InputStream unfiltered;
    public void setUnfilteredStream(InputStream unfiltered) {
        this.unfiltered = unfiltered;
    }
    private OutputStream threadOutput;
    public void setThreadOutputStream(OutputStream threadOutput) {
        this.threadOutput = threadOutput;
    }    

    // read from unfiltered stream, filter and write to thread output stream
    public abstract void run();
}

...

public InputStream getFilteredStream(InputStream unfiltered, FilterThread filter) {
    PipedInputStream filteredInputStream = new PipedInputStream();
    PipedOutputStream threadOutputStream = new PipedOutputStream(filteredInputStream);

    filter.setUnfilteredStream(unfiltered);
    filter.setThreadOuptut(threadOutputStream);
    filter.start();

    return filteredInputStream;
}

...

public void clientCode() {
    ...
    ZipInputStream zis = ...;// get ZIP stream
    FilterThread filter = ...; // assign your implementation of FilterThread that transforms your ZipInputStream

    InputStream filteredZipInputStream = getFilteredStream(zis, filter);
    ...
}

答案 4 :(得分:-1)

邮政编码相当简单,但我将ZipInputStream作为Inputstream返回时出现问题。由于某种原因,zip中包含的某些文件会删除字符。以下是我的解决方案,到目前为止一直在运作。

private Map<String, InputStream> getFilesFromZip(final DataHandler dhZ,
        String operation) throws ServiceFault
{
    Map<String, InputStream> fileEntries = new HashMap<String, InputStream>();
    try
    {

        DataSource dsZ = dhZ.getDataSource();

        ZipInputStream zipIsZ = new ZipInputStream(dhZ.getDataSource()
                .getInputStream());

        try
        {
            ZipEntry entry;
            while ((entry = zipIsZ.getNextEntry()) != null)
            {
                if (!entry.isDirectory())
                {
                    Path p = Paths.get(entry.toString());
                    fileEntries.put(p.getFileName().toString(),
                            convertZipInputStreamToInputStream(zipIsZ));
                }

            }
        }
        finally
        {
            zipIsZ.close();
        }

    }
    catch (final Exception e)
    {
        faultLocal(LOGGER, e, operation);
    }

    return fileEntries;
}
private InputStream convertZipInputStreamToInputStream(
        final ZipInputStream in) throws IOException
{
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    IOUtils.copy(in, out);
    InputStream is = new ByteArrayInputStream(out.toByteArray());
    return is;
}