Solved

Uncompress zipped data

Posted on 2002-06-13
13
224 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
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 
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

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!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Unhandled exception type Exception 18 52
jboss wildfly 10.1 10 437
going to wrong jsp page 2 50
Bitwise and to sum elements 2 14
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
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.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

730 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