Solved

Uncompress zipped data

Posted on 2002-06-13
13
225 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
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

 
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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
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.

726 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