8

I have a zip file and after decoding it I get a byte array now I want to create a FileInputStream object with that byte[] object. I dont want to create a file instead pass data content do FileInputStream. Is there any way ?

following is the code:

byte[] decodedHeaderFileZip = decodeHeaderZipFile(headerExportFile);
FileInputStream fileInputStream = new FileInputStream(decodedHeaderZipFileString);

EDIT: I wanted to build a ZipInputStream object with a FileInputStream.

Rauf
  • 620
  • 1
  • 8
  • 20

1 Answers1

38

I have a zip file and after decoding it I get a byte array now I want to create a FileInputStream object with that byte[] object.

But you don't have a file. You have some data in memory. So a FileInputStream is inappropriate - there's no file for it to read from.

If possible, use a ByteArrayInputStream instead:

InputStream input = new ByteArrayInputStream(decodedHeaderFileZip);

Where possible, express your API in terms of InputStream, Reader etc rather than any specific implementation - that allows you to be flexible in which implementation you use. (What I mean is that where possible, make method parameters and return types InputStream rather than FileInputStream - so that callers don't need to provide the specific types.)

If you absolutely have to create a FileInputStream, you'll need to write the data to a file first.

BrTkCa
  • 4,703
  • 3
  • 24
  • 45
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • thanks..but can you please explain "express your API in terms of InputStream, Reader etc rather than any specific implementation - that allows you to be flexible in which implementation you use." – Rauf Nov 07 '13 at 07:23
  • 1
    as mentioned in EDIT..with @Jon Skeets help I formed InputStream object and passed it to the ZipInputStream as a parameter and IT WORKED !! thank you :-) – Rauf Nov 07 '13 at 07:32
  • 1
    @Rauf: I've edited my answer to explain what I mean about APIs. It's not clear whether you *did* have an API which needed `FileInputStream` at all, but it's still something to consider. – Jon Skeet Nov 07 '13 at 08:12