Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Load variable from java InputStream

40 views
Skip to first unread message

Ray

unread,
Sep 2, 2010, 5:29:05 PM9/2/10
to
I'm picking out a single file from a zip file and creating a java InputStream which points to it using the following code:
PathName = 'somepath';
FileName = 'someZipFile';
% Open the zip file
zipJavaFile = java.io.File(strcat(PathName,FileName));
zipFile = org.apache.tools.zip.ZipFile(zipJavaFile);
% Pick out just the results data file.
results_file = zipFile.getEntry('results.mat');
% Set up the stream
results_stream = zipFile.getInputStream(results_file)

So now I'd like to read the contents of this MAT file into the variable workspace. I'm stumped (obviously). I can use the java "read" function to get the contents, but have no way to parse the raw data. Do I really need to use the unzip.m function to put the contents of the file into a temp directory and then use the built-in load function to load from that? Is there any way to trick "load" into using a java InputStream object instead of a string?

Thanks!

Ray

unread,
Sep 14, 2010, 11:48:06 AM9/14/10
to
Replying to my own message...

I've been running for a week or so just unzipping to a temporary directory. It doesn't feel as "clean", but it seems to be working fine and the performance is satisfactory.

Thanks!
Ray

Jamie

unread,
Nov 15, 2012, 5:34:17 PM11/15/12
to
I was looking at the same problem and discovered a solution that uses java.io.ByteArrayOutputStream. This is an output stream that gathers bytes and then you can request them as a byte array. I use streamCopier just like it is used in extractArchive.m, to copy from the zip entry to the ByteArrayOutputStream.

entry = zipFile.getEntry('myentry.txt');
stream = zipFile.getInputStream(entry);
byteoutstream = java.io.ByteArrayOutputStream();
streamCopier = com.mathworks.mlwidgets.io.InterruptibleStreamCopier.getInterruptibleStreamCopier;
streamCopier.copyStream(stream, byteoutstream);
sbytes = byteoutstream.toByteArray();
ubytes = uint8(int16(sbytes) + 256*int16(sbytes < 0));

Since toByteArray() returns (signed) int8 and I want uint8, I manually apply the wrap-around at the end.

-Jamie
0 new messages