Right now I'm working on an archive browsing application that lets users navigate through archive contents, extract the archives and preview the files inside the archive. I'm using java.util.zip
API. To preview a file I'm extracting it temporarily and opening it as a usual existing file. As you may understand, that's not a good approach since it won't preview files if there's not enough space to make a temporary extraction. Is there a working solution for passing ZipInputStream
to an Activity
to open it as a file? Is there another workaround for this problem? Thanks in advance.

- 39,695
- 10
- 113
- 130
-
Is it not possible to retrieve just the entries with the entries() method and one the user has chosen one entry retrieve it with the getInputStream(entry) from the JDK 1.6 api it looks like you can. – PbxMan Oct 12 '12 at 17:05
1 Answers
In principle, you could create a ContentProvider
that serves up the ZipInputStream
.
In this sample project I demonstrate how to create a ContentProvider
supporting openFile()
that uses a pipe created by ParcelFileDescriptor.createPipe()
to serve up a file. createPipe()
returns a pair (two-element array) of ParcelFileDescriptors
representing the ends of the pipe. You use the second element out of the array to write to via an OutputStream
; openFile()
returns the first element out of the array to be passed by Android to the calling process. The caller would use openInputStream()
to read in what you transfer via the pipe.
In my case, I am sending an asset on which I get an InputStream
via AssetManager
. In your case, you would use your ZipInputStream
.
Note that my sample project assumes it is being run on a device that has a PDF viewer, since it is serving a PDF out of assets and trying to open it via startActivity()
.

- 986,068
- 189
- 2,389
- 2,491
-
-
2The approach worked fine when I've tried to open an image from archive, which is 6KB in size. When trying to open a bigger file - it gives me an IOException: write failed: EPIPE (Broken pipe). Can you suggest what may be the case here? Thank you. – Egor Oct 15 '12 at 15:04
-
1@Egor: There's a 64KB buffer limit on the pipe, though you should be able to work around that: http://stackoverflow.com/questions/11138696/android-parcelfiledescriptor-createpipe-method-64kb-error – CommonsWare Oct 15 '12 at 15:13
-
As I see, this workaround logic is implemented in your code, then why won't it work for me? – Egor Oct 15 '12 at 15:42
-
@Egor: I do not know. For a moment, I thought it was just that I had chosen a small test PDF, and that I too would crash on a larger one. But I just tried a 230KB one, and I had no problems. – CommonsWare Oct 15 '12 at 15:58
-
1Another thing I've figured out is that the approach works fine for images of larger sizes, but crashes for audio and video files. Also I've seen the "Unable to create media player" error in logcat. So I presume there's a problem with using this solution with MediaPlayer. Can it be the case and are there any workarounds you can think of? Thank you. – Egor Oct 16 '12 at 11:41
-
@Egor: It is certainly possible that `MediaPlayer` has issues, though off the top of my head I can't think of what they'd be, over any other possible client of the stream. Make sure that `getType()` in your provider is returning the correct MIME type for the contents. – CommonsWare Oct 16 '12 at 12:37
-
let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/18108/discussion-between-egor-and-commonsware) – Egor Oct 16 '12 at 12:42
-
@Egor: Well, I was able to reproduce your problem with `createPipe()` and `MediaPlayer`, this time with a simple OGG clip in `assets/`. I have [opened my own question](http://stackoverflow.com/questions/12920429/anyone-have-mediaplayer-working-with-parcelfiledescriptor-and-createpipe) on this topic to help drive interest, in part because I can supply full sample code that fails. You might want to keep tabs on it to see if I find an answer. – CommonsWare Oct 16 '12 at 17:42
-
-
Mark, thank you for the answer and for the sample! I used it to return a filtered string from a database column as a file. – Y2i Jul 23 '13 at 06:44