Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

about to the ZipInputStream and JPG

 
Lawrance Miao
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9300
10
Linux Mac OS X Windows
 
Lawrance Miao
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9300
10
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, Joe Ess. I think I'd be better to read that article carefully.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic