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
Solved

Uncompress zipped data

Posted on 2002-06-13
13
223 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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone 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

Suggested Solutions

Title # Comments Views Activity
Should localization be done inside spring controller 5 32
check java version using powershell 13 177
collection output issue 9 37
running on tomcat not jboss eap 7.0 3 18
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

828 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