wood burning stoves 2.0*
The moose likes I/O and Streams and the fly likes about to the ZipInputStream and JPG Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "about to the ZipInputStream and JPG" Watch "about to the ZipInputStream and JPG" New topic
Author

about to the ZipInputStream and JPG

Lawrance Miao
Greenhorn

Joined: Jan 24, 2005
Posts: 8
Hi all
Today I tried to use ZipInputStream and ZipOutputStream to write a program like Winzip. But when I decompressed a .jpg file from the zip file into a common file, the problem appeared. The image file is totally changed.

My method is:

zin = new ZipInputStream(new FileInputStream(zipFileName));
while ((entry = zin.getNextEntry()) != null)
{
if (entry.getName().contains("hello.jpg"))
{
String fileName = entry.getName();
FileOutputStream out = new FileOutputStream("helloCopy.jpg");
byte[] b = new byte[(int)entry.getSize()];
zin.read(b);
out.write(b);
out.close();
}
zin.closeEntry();
}
zin.close();

This method was almost right when I used other kind of file formats not including images. I really puzzled!!

Is there any solutions??
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8935
    
    9

Read Doesn't Do What You Think It Does


"blabbing like a narcissistic fool with a superiority complex" ~ N.A.
[How To Ask Questions On JavaRanch]
Lawrance Miao
Greenhorn

Joined: Jan 24, 2005
Posts: 8
First, thanks Joe Ess. That article is very impressing.

But, that is not the solution I think. Because the inputstream is a little different from that one. I've successed to copy one jpg to another one if the jpg image is not in the zip file.

I've found the solution when I'm idling in the JDK docs:

import java.io.*;
import java.util.*;
import java.util.zip.*;
import com.sun.image.codec.jpeg.*; //solution.
import java.awt.image.*;

public class TestZipImage
{
public static void main(String args[])
{
try{
ZipInputStream zin =
new ZipInputStream(new FileInputStream("t10001.zip"));
ZipEntry entry;
while ((entry = zin.getNextEntry()) != null)
{
if (entry.getName().contains("t10001.jpg"))
{
String fileName = entry.getName();
FileOutputStream out = new FileOutputStream("copy_t10001.jpg");
//--Work Code------------------------------------------------------
JPEGImageDecoder jpgDec = JPEGCodec.createJPEGDecoder(zin);
BufferedImage bimage = jpgDec.decodeAsBufferedImage();
JPEGImageEncoder jpgEnd = JPEGCodec.createJPEGEncoder(out);
jpgEnd.encode(bimage);
out.flush();
//---Work Code---------------------------------------------------

//---Not Work Code-----------------------------------------------
// byte[] b = new byte[(int)entry.getSize() * 10];
// zin.read(b);
// out.write(b);
//---Not Work Code-----------------------------------------------

out.close();
}
zin.closeEntry();
}
zin.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}

I think the reason is the format of the extracting of jpg files.
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8935
    
    9

Originally posted by Lawrance Miao:

I think the reason is the format of the extracting of jpg files.

The reason why JPEGImageDecoder works is because it reads the data fully in order to create a BufferedImage instance. Using the graphics subsystem is a very inefficient way to move data around.
When it comes to moving data, it doesn't matter if the data is a text file or a JPEG and it doesn't matter if the source is a zip file or a socket. Use the read() method correctly and your problem will be solved.
Lawrance Miao
Greenhorn

Joined: Jan 24, 2005
Posts: 8
Thanks, Joe Ess. I think I'd be better to read that article carefully.
 
 
subject: about to the ZipInputStream and JPG