Solved

Uncompress zipped data

Posted on 2002-06-13
13
216 Views
Last Modified: 2012-06-27
My database table contains a BLOB column which contains binary data in zipped format. I would like to retrieve this data, uncompress it, and write out the unzipped data to (1) a byte array, and (2) a file. The data from the table is retrieved as follows:

InputStream inputStream = resultSet.getBinaryStream(1);

where resultSet is a ResultSet object.

Thanks!
0
Comment
Question by:yongsing
13 Comments
 
LVL 92

Expert Comment

by:objects
ID: 7077492
Try something like:

ZipInputStream zin = new ZipInputStream(in);
entry = zin.getNextEntry();
FileOutputStream out = new FileOutputStream(entry.getName());  // you may need to change this depending on your requirements
long size = entry.getSize();
byte[] buf = new byte[(int)size];
zin.read(buf);
out.write(buf);
out.close();
in.close();
0
 
LVL 9

Author Comment

by:yongsing
ID: 7077592
I've tried something similar to yours, but found that entry.getSize() returns -1.
0
 
LVL 92

Expert Comment

by:objects
ID: 7077610
Are you sure its zipped?  How was it compressed?
0
 
LVL 92

Expert Comment

by:objects
ID: 7077613
Try simply reading the data using an InflaterInputStream.
0
 
LVL 9

Author Comment

by:yongsing
ID: 7077666
I've tried something similar to yours, but found that entry.getSize() returns -1.
0
 
LVL 9

Author Comment

by:yongsing
ID: 7077674
Sorry for the duplicate post. I accidentally clicked the Refresh button. Yes, the data is indeed zipped using ZippedOutputStream and stored into the database. I have retrieved the compressed data from the database and written it out directly to a file, and then using winzip to unzip it without problems. Anyway, here's the code to zip it and store into the database:

ByteArrayOutputStream outputStream = new ByteArrayOutputStream(1000);
ZipOutputStream zipOutputStream = new ZipOutputStream(outputStream);
ZipEntry zipEntry = new ZipEntry(serial);
try {
  zipOutputStream.putNextEntry(zipEntry);
  zipOutputStream.write(rawData, 0, rawData.length);
  zipOutputStream.closeEntry();
  zipOutputStream.close();
} catch (IOException ioException) {
  // some code here
}
byte[] zippedRawData = outputStream.toByteArray();

// Insert the data into the database
try {        
   // some code here

   preparedStatement.setBinaryStream(3, new BufferedInputStream(new ByteArrayInputStream(zippedRawData)), zippedRawData.length);

   preparedStatement.executeUpdate();
   connection.commit();
} catch (SQLException sqlException) {
}
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 9

Author Comment

by:yongsing
ID: 7077688
This is how I retrieve the compressed data from the database and write it out to a ZIP file:

BufferedInputStream inputStream = new BufferedInputStream(resultSet.getBinaryStream(1));
FileOutputStream outputStream = new FileOutputStream(serial + ".zip");
int b;
while ((b = inputStream.read()) != -1) outputStream.write(b);
inputStream.close();
outputStream.close();

There's no problem with the above code. Please let me know if there's a better way.
0
 
LVL 9

Author Comment

by:yongsing
ID: 7077833
I finally got it right. First of all, retrieve the data from the database into an array first:

BufferedInputStream inputStream = new BufferedInputStream(resultSet.getBinaryStream(3));
ByteArrayOutputStream outputStream = new ByteArrayOutputStream(1000);
int b;
while ((b = inputStream.read()) != -1) {
  outputStream.write(b);
}
inputStream.close();
byte[] zippedRawData = outputStream.toByteArray();
outputStream.close();

Then uncompress the data into a file:

FileOutputStream fileOutputStream = new FileOutputStream("unzipped.xxx");
ZipInputStream zipInputStream = new ZipInputStream(new ByteArrayInputStream(zippedRawData));
ZipEntry zipEntry = zipInputStream.getNextEntry();
byte[] rawData = new byte[1024];
int bytesRead;
while ((bytesRead = zipInputStream.read(rawData, 0, 1024)) != -1) {
  fileOutputStream.write(rawData, 0, bytesRead);
}
zipInputStream.close();
fileOutputStream.close();
0
 
LVL 92

Expert Comment

by:objects
ID: 7077834
> but found that entry.getSize() returns -1.

javadoc actually states that this means unknown so it may still be ok.

Try the following:

ZipInputStream zin = new ZipInputStream(in);
entry = zin.getNextEntry();
FileOutputStream out = new FileOutputStream(entry.getName());  // you may need to change this depending
on your requirements
int b;
while ((b = zin.read()) != -1) out.write(b);
out.close();
in.close();
0
 
LVL 9

Author Comment

by:yongsing
ID: 7759999
Sorry for abandoning this question till now. Actually, I found out the answers to my question myself (as you can see from my post). Is it ok if I ask community support to delete this question?

Thanks for your help anyway.
0
 
LVL 92

Expert Comment

by:objects
ID: 7764841
Same effect as what I suggested :)
Feel free to delete.
0
 
LVL 1

Expert Comment

by:Computer101
ID: 7767100
A request for deletion has been made.  If no response or you feel this is in error, comment.  If no objection, I will delete in three days.

Computer101
E-E Admin
0
 
LVL 6

Accepted Solution

by:
Mindphaser earned 0 total points
ID: 7839803
Points refunded and moved to PAQ

** Mindphaser - Community Support Moderator **
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now