?
Solved

Uncompress zipped data

Posted on 2002-06-13
13
Medium Priority
?
226 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This video teaches viewers about errors in exception handling.
Suggested Courses
Course of the Month7 days, 19 hours left to enroll

765 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