Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Load variable from java InputStream

Subject: Load variable from java InputStream

From: Ray

Date: 2 Sep, 2010 21:29:05

Message: 1 of 3

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!

Subject: Load variable from java InputStream

From: Ray

Date: 14 Sep, 2010 15:48:06

Message: 2 of 3

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

Subject: Load variable from java InputStream

From: Jamie

Date: 15 Nov, 2012 22:34:17

Message: 3 of 3

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

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us