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.

1 Answer 1

42

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.

Sign up to request clarification or add additional context in comments.

3 Comments

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."
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: 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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.