Page 1 of 2 12 LastLast
Results 1 to 20 of 30
  1. #1
    JavaN00bie is offline Member
    Join Date
    Jun 2011
    Posts
    21
    Rep Power
    0

    Question 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!

  2. #2
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,641
    Rep Power
    7

    Default

    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.

  3. #3
    JavaN00bie is offline Member
    Join Date
    Jun 2011
    Posts
    21
    Rep Power
    0

    Default

    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();
    	        }
    		}

  4. #4
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,641
    Rep Power
    7

    Default

    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)

  5. #5
    JavaN00bie is offline Member
    Join Date
    Jun 2011
    Posts
    21
    Rep Power
    0

    Default

    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.

  6. #6
    JavaN00bie is offline Member
    Join Date
    Jun 2011
    Posts
    21
    Rep Power
    0

    Default

    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-16-2011 at 12:13 AM.

  7. #7
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,453
    Rep Power
    20

    Default

    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.
    Java 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));
      }
    }
    db

  8. #8
    JavaN00bie is offline Member
    Join Date
    Jun 2011
    Posts
    21
    Rep Power
    0

    Default

    So I tried to incorporate your code into what I had. The first try, I tried to add iteration without error checking...

    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));
    }
    ....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:
    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...

    Java 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();
    }
    }
    And the error I get is basically like

    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 05:35 PM.

  9. #9
    JavaN00bie is offline Member
    Join Date
    Jun 2011
    Posts
    21
    Rep Power
    0

    Default

    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 :/

  10. #10
    jing-yi is offline Member
    Join Date
    May 2011
    Posts
    35
    Rep Power
    0

    Default

    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.

  11. #11
    jing-yi is offline Member
    Join Date
    May 2011
    Posts
    35
    Rep Power
    0

    Default

    Ya, you should not face FileNotFoundException.

  12. #12
    JavaN00bie is offline Member
    Join Date
    Jun 2011
    Posts
    21
    Rep Power
    0

    Default

    Which of those three versions of code did you try?

  13. #13
    jing-yi is offline Member
    Join Date
    May 2011
    Posts
    35
    Rep Power
    0

    Default

    The last bunch of code in this ticket. Please add
    Java Code:
    JOptionPane.showMessageDialog(null, new ImageIcon(image));
    after
    Java Code:
    ImageIO.write((RenderedImage) image, "jpeg", outputfile);
    . Bear in mind that
    Java Code:
    while (!zipEntry.isDirectory())
    may cause infinite loop.

  14. #14
    JavaN00bie is offline Member
    Join Date
    Jun 2011
    Posts
    21
    Rep Power
    0

    Default

    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....

    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();
    			}
    		}
    	}
    }
    No FileNotFoundException...but I did get:

    Exception in thread "main" java.lang.NullPointerException
    at ThisIsTest.main(ThisIsTest.java:38)
    It occurs at: "BufferedImage bufferedImage = new BufferedImage(image.getWidth(null),image.getHeight (null),BufferedImage.TYPE_INT_RGB);"

    I have no clue how this worked for you.

  15. #15
    jing-yi is offline Member
    Join Date
    May 2011
    Posts
    35
    Rep Power
    0

    Default

    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?

  16. #16
    JavaN00bie is offline Member
    Join Date
    Jun 2011
    Posts
    21
    Rep Power
    0

    Default

    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?

  17. #17
    jing-yi is offline Member
    Join Date
    May 2011
    Posts
    35
    Rep Power
    0

    Default

    Please check those zip files. One of them consist no image.

  18. #18
    JavaN00bie is offline Member
    Join Date
    Jun 2011
    Posts
    21
    Rep Power
    0

    Default

    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.

  19. #19
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,641
    Rep Power
    7

    Default

    It occurs at: "BufferedImage bufferedImage = new BufferedImage(image.getWidth(null),image.getHeight (null),BufferedImage.TYPE_INT_RGB);"
    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 ImageIcon
    Last edited by doWhile; 06-16-2011 at 08:26 PM.

  20. #20
    jing-yi is offline Member
    Join Date
    May 2011
    Posts
    35
    Rep Power
    0

    Default

    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 08:26 PM.

Page 1 of 2 12 LastLast

Similar Threads

  1. Quick rotating images question
    By sgthale in forum New To Java
    Replies: 5
    Last Post: 04-10-2011, 06:47 AM
  2. Replies: 1
    Last Post: 03-26-2010, 11:41 PM
  3. question on images
    By munigantipraveen in forum JDBC
    Replies: 1
    Last Post: 07-21-2008, 09:07 PM
  4. question on images
    By munigantipraveen in forum JDBC
    Replies: 1
    Last Post: 06-27-2008, 05:13 PM
  5. Zip Archive Problem
    By satishbejgum in forum Advanced Java
    Replies: 1
    Last Post: 12-20-2007, 10:08 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •