Results 1 to 20 of 30
- 06-15-2011, 06:47 PM #1
Member
- Join Date
- Jun 2011
- Posts
- 21
- Rep Power
- 0
Question About Extracting Images From Zip Archive
Hi everyone, I'm fairly new to Java but I was trying to create an application that would look through a directory of zip files and go into the archives of each individual zip file and extract only the images stored within the archive, not other documents or text.
I know how to get into the zip file, as well as how to loop through the individual entries in the zip file, but I have no idea how to read the images or even how to extract them and store them in a separate directory.
Could anyone offer me some help with this problem or at least point me in the right direction? It would be greatly appreciated. Thank you very much in advance!
- 06-15-2011, 09:42 PM #2
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,609
- Rep Power
- 5
ImageIO (Java Platform SE 6) provides read/write functions for images. In the case of reading, it will throw an exception if the file cannot be read as an image, which if thrown essentially tells you the file is not a readable image file format.
- 06-15-2011, 09:58 PM #3
Member
- Join Date
- Jun 2011
- Posts
- 21
- Rep Power
- 0
Yeah, I actually just started reading up on imageIO a few hours before you posted.
However, I'm trying to parse through the entries in the zip file. And this is what I tried to do (code-wise), but the errors were endless :/.
Java Code:for (File f : files) { try { String fileName = f.getName(); // Create an instance of ZipFile to read a zip file ZipFile zip = new ZipFile(new File(fileName)); FileInputStream fis=new FileInputStream(fileName); BufferedInputStream bis=new BufferedInputStream(fis); ZipInputStream zis=new ZipInputStream(bis); ZipEntry ze=null; while ((ze=zis.getNextEntry())!=null) { if (ze.isDirectory()) { BufferedImage image = null; ImageWriter writer = null; image = (BufferedImage) ImageIO.getImageReadersByFormatName("jpeg"); writer.setOutput("C:/Documents and Settings/rajeeva/My Documents/JavaTest/Test/" + image); } else { break; } } } catch (ZipException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }
- 06-15-2011, 10:05 PM #4
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,609
- Rep Power
- 5
What errors? Compile time? Run time? The getImageReadersByFormatName() method returns an Iterator, not a BufferedImage. Take a step back and break the problem down. First loop through the files of the Zip file (there are plenty of examples online for how to do this). Next, figure out how to read said files (see ZipFile.getInputSteam). Lastly, get the InputStream of files that are not directories, and call ImageIO.read(InputStream)
- 06-15-2011, 10:26 PM #5
Member
- Join Date
- Jun 2011
- Posts
- 21
- Rep Power
- 0
Yeah, it was a bunch of run time errors. I'm going to take your advice and go back to the drawing board here, hopefully I can break this down better on my second try.
- 06-15-2011, 11:11 PM #6
Member
- Join Date
- Jun 2011
- Posts
- 21
- Rep Power
- 0
Okay, so I went back to the drawing board and I feel like I have now gotten it to fully loop through the zip file, identify it's contents and read them, and then identify the not directories...but I have no idea what to make the imageIO read in order to output the image I want. Any help here? (Just to note, anything within that while loop is pretty much a guess, so apologies lol.)
Edit: I get a compile time error where it says "image = ImageIO.read(zipReader);".....the "read" part is underlined :/
Java Code:for (File f : files) { try { String fileName = f.getName(); ZipFile zipFile = new ZipFile(fileName); for (Enumeration e = zipFile.entries(); e.hasMoreElements();) { ZipEntry zipEntry = (ZipEntry) e.nextElement(); //use BufferedReader to get one line at a time BufferedReader zipReader = new BufferedReader(new InputStreamReader(zipFile.getInputStream(zipEntry))); //while the entry is not a directory while (!zipEntry.isDirectory()) { BufferedImage image = null; image = ImageIO.read(zipReader); String strNewFileName = zipReader.readLine(); File outputfile = new File("C:/Documents and Settings/" + strNewFileName); ImageIO.write(image, "jpeg", outputfile); ; } zipReader.close(); } } catch (ZipException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }Last edited by JavaN00bie; 06-15-2011 at 11:13 PM.
- 06-16-2011, 08:10 AM #7
A Reader is used for character data, not binary data. Nutshell example without error checking or iteration, which you can run and test with a zip file that contains only one image file.
dbJava Code:import java.awt.Image; import java.io.InputStream; import java.util.zip.ZipEntry; import java.util.zip.ZipFile; import javax.imageio.ImageIO; import javax.swing.ImageIcon; import javax.swing.JOptionPane; public class ImageFromZip { public static void main(String[] args) throws Exception{ ZipFile zipFile = new ZipFile("E:/images.zip"); ZipEntry zipEntry = zipFile.entries().nextElement(); InputStream entryStream = zipFile.getInputStream(zipEntry); Image image = ImageIO.read(entryStream); JOptionPane.showMessageDialog(null, new ImageIcon(image)); } }
- 06-16-2011, 04:27 PM #8
Member
- Join Date
- Jun 2011
- Posts
- 21
- Rep Power
- 0
So I tried to incorporate your code into what I had. The first try, I tried to add iteration without error checking...
....I got no compile time errors, but I still got some run-time errors and no dialog box came up with any images. So I thought to try it with just a singular image like you had said....Java Code:for (File f : files) { String fileName = f.getName(); ZipFile zipFile = new ZipFile("C:/Documents and Settings/" + fileName); ZipEntry zipEntry = zipFile.entries().nextElement(); InputStream entryStream = zipFile.getInputStream(zipEntry); Image image = ImageIO.read(entryStream); JOptionPane.showMessageDialog(null, new ImageIcon(image)); }
Java Code:String fileName = files[0].getName(); ZipFile zipFile = new ZipFile("C:/Documents and Settings/" + fileName); ZipEntry zipEntry = zipFile.entries().nextElement(); InputStream entryStream = zipFile.getInputStream(zipEntry); Image image = ImageIO.read(entryStream); JOptionPane.showMessageDialog(null, new ImageIcon(image));
...Still the same run-time error and no image produced. Now, I knew this probably wouldn't work, but I wanted to be able to extract the images and create separate jpeg files for each of them (not display them in a pop-up window like your code did), so I tried something like this...
And the error I get is basically likeJava Code:for (File f : files) { try { String fileName = f.getName(); ZipFile zipFile = new ZipFile("C:/Documents and Settings/" + fileName); for (Enumeration e = zipFile.entries(); e.hasMoreElements();) { ZipEntry zipEntry = zipFile.entries().nextElement();; //while the entry is not a directory while (!zipEntry.isDirectory()) { InputStream entryStream = zipFile.getInputStream(zipEntry); Image image = ImageIO.read(entryStream); BufferedImage bufferedImage = new BufferedImage(image.getWidth(null),image.getHeight(null),BufferedImage.TYPE_INT_RGB); String strNewFileName = zipEntry.getName(); File outputfile = new File("C:/Documents and Settings/" + strNewFileName); ImageIO.write((RenderedImage) image, "jpeg", outputfile); } } } catch (ZipException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }
Exception in thread "main" java.io.FileNotFoundException: C:\Documents and Settings\Document.docx (The system cannot find the file specified)
at java.util.zip.ZipFile.open(Native Method)
at java.util.zip.ZipFile.<init>(Unknown Source)
at java.util.zip.ZipFile.<init>(Unknown Source)
at Test.main(Test.java:68)
No idea why because that file does exist where I said it was located :/
I have more code, prior to this, but I don't know if it would be helpful or not as it does something different (but it works...I think, lol).
Anyone have any ideas?Last edited by JavaN00bie; 06-16-2011 at 04:35 PM.
- 06-16-2011, 06:05 PM #9
Member
- Join Date
- Jun 2011
- Posts
- 21
- Rep Power
- 0
I was just messing around with some things in my code and I was able to get rid of the FileNotFoundException by creating a new array of files pointing to another directory, however I still can't produce any images and the code DarrylBurke provided still returns a null pointer exception :/
- 06-16-2011, 06:06 PM #10
Member
- Join Date
- May 2011
- Posts
- 35
- Rep Power
- 0
I try your code in my side and it is working fine.
Go to start>run>type C:\Documents and Settings\Document.docx.
and see whether you can open the file or not.
- 06-16-2011, 06:08 PM #11
Member
- Join Date
- May 2011
- Posts
- 35
- Rep Power
- 0
Ya, you should not face FileNotFoundException.
- 06-16-2011, 06:10 PM #12
Member
- Join Date
- Jun 2011
- Posts
- 21
- Rep Power
- 0
Which of those three versions of code did you try?
- 06-16-2011, 06:18 PM #13
Member
- Join Date
- May 2011
- Posts
- 35
- Rep Power
- 0
The last bunch of code in this ticket. Please add
afterJava Code:JOptionPane.showMessageDialog(null, new ImageIcon(image));
. Bear in mind thatJava Code:ImageIO.write((RenderedImage) image, "jpeg", outputfile);
may cause infinite loop.Java Code:while (!zipEntry.isDirectory())
- 06-16-2011, 06:36 PM #14
Member
- Join Date
- Jun 2011
- Posts
- 21
- Rep Power
- 0
I have no idea how you got this code to work, lol.
I tried to create a separate java project and run this code separate from the other code I had....
No FileNotFoundException...but I did get:Java Code:import java.awt.Image; import java.awt.image.BufferedImage; import java.awt.image.RenderedImage; import java.io.File; import java.io.IOException; import java.io.InputStream; import java.util.Enumeration; import java.util.zip.ZipEntry; import java.util.zip.ZipException; import java.util.zip.ZipFile; import javax.imageio.ImageIO; import javax.swing.ImageIcon; import javax.swing.JOptionPane; public class ThisIsTest { public static void main(String[] args) { File zipDirectory = new File("C:/Documents and Settings/"); File files2[] = zipDirectory.listFiles(); for (File f : files2) { try { String fileName = f.getName(); ZipFile zipFile = new ZipFile("C:/Documents and Settings/" + fileName); for (Enumeration e = zipFile.entries(); e.hasMoreElements();) { ZipEntry zipEntry = zipFile.entries().nextElement();; //while the entry is not a directory while (!zipEntry.isDirectory()) { InputStream entryStream = zipFile.getInputStream(zipEntry); Image image = ImageIO.read(entryStream); BufferedImage bufferedImage = new BufferedImage(image.getWidth(null),image.getHeight(null),BufferedImage.TYPE_INT_RGB); String strNewFileName = zipEntry.getName(); File outputfile = new File("C:/Documents and Settings/" + strNewFileName); ImageIO.write((RenderedImage) image, "jpeg", outputfile); JOptionPane.showMessageDialog(null, new ImageIcon(image)); } } } catch (ZipException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } } }
It occurs at: "BufferedImage bufferedImage = new BufferedImage(image.getWidth(null),image.getHeight (null),BufferedImage.TYPE_INT_RGB);"Exception in thread "main" java.lang.NullPointerException
at ThisIsTest.main(ThisIsTest.java:38)
I have no clue how this worked for you.
- 06-16-2011, 06:49 PM #15
Member
- Join Date
- May 2011
- Posts
- 35
- Rep Power
- 0
Can you tell me how many files in C:/Documents and Settings/ at your side? Do you able to zip them and send it over?
- 06-16-2011, 06:57 PM #16
Member
- Join Date
- Jun 2011
- Posts
- 21
- Rep Power
- 0
Well it's not 'exactly' in "C:/Documents and Settings/", there is a longer extension but I wasn't trying to reveal personal information. Within the directory that I am trying to access there are 4 zip files that I can go into with Winzip and just look through the archives. And I would send them over, but again they contain some sensitive information so I really can't do that. :/
How many zip files are on your side?
- 06-16-2011, 07:00 PM #17
Member
- Join Date
- May 2011
- Posts
- 35
- Rep Power
- 0
Please check those zip files. One of them consist no image.
- 06-16-2011, 07:07 PM #18
Member
- Join Date
- Jun 2011
- Posts
- 21
- Rep Power
- 0
Just checked, all of them contain at least one image file.
Maybe I should give some more detail as to what I'm doing...
So I basically am bulk converting a bunch of pre-word 2007 documents to docx format using an external application I run from the command line (works fine). Then I am changing the extension on all of those .docx files to .zip and storing them in a new folder (works fine). Then I am trying to go into each of those zip files and access the directory "word/media/" and extract all the images (.jpegs only) from them.
Don't know if that's of any help, but just thought I'd put it out there.
- 06-16-2011, 07:23 PM #19
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,609
- Rep Power
- 5
First, read the API for ImageIO.read....it is defined to return a BufferedImage OR null...so do double check the returned reference is not null. Second, any reason why you are creating a BufferedImage that is never used? ImageIO returns a BufferedImage (if the read InputSteam/File/URL is an image), which can then be passed to write (no cast is necessary) or used to create an ImageIconIt occurs at: "BufferedImage bufferedImage = new BufferedImage(image.getWidth(null),image.getHeight (null),BufferedImage.TYPE_INT_RGB);"Last edited by doWhile; 06-16-2011 at 07:26 PM.
- 06-16-2011, 07:24 PM #20
Member
- Join Date
- May 2011
- Posts
- 35
- Rep Power
- 0
Do you mean you want to extract images in jpeg format only from those zip files?
Let say in c:\directory consist of 2 zip files, which are fileA.zip and fileB.zip.
fileA.zip consist of a.docs, b.img, c.txt,d.jpg,e.jpeg.
fileB.zip consist of f.xls, g.jpeg
My understanding is you want extract e.jpeg and g.jpeg only to c:\directory and ignore the rest. How bout d.jpg?
If possible can you write the file skeleton inside the 4 zip files? (I understand that those files are sensitive).Last edited by jing-yi; 06-16-2011 at 07:26 PM.
Similar Threads
-
Quick rotating images question
By sgthale in forum New To JavaReplies: 5Last Post: 04-10-2011, 05:47 AM -
Question about loading images in a WebStart application
By Psyclone in forum AWT / SwingReplies: 1Last Post: 03-26-2010, 10:41 PM -
question on images
By munigantipraveen in forum JDBCReplies: 1Last Post: 07-21-2008, 08:07 PM -
question on images
By munigantipraveen in forum JDBCReplies: 1Last Post: 06-27-2008, 04:13 PM -
Zip Archive Problem
By satishbejgum in forum Advanced JavaReplies: 1Last Post: 12-20-2007, 09:08 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks