Page 2 of 2 FirstFirst 12
Results 21 to 30 of 30
  1. #21
    jing-yi is offline Member
    Join Date
    May 2011
    Posts
    35
    Rep Power
    0

    Default

    DoWhile's point is correct. Please remove it.

    For your information, the code that you post actually cannot handle zip files which consist of non-image files. For example file.zip (a.txt, b.jpeg).

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

    Default

    I have no idea why that was there to be honest, I think may have been left in from one of my past attempts? I got rid of it though, and made some slight modifications to my code. Now I'm not getting any errors...but I think I may be stuck in an infinite loop, I just don't know for sure because maybe I just have a crappy computer.

    Could anyone test this could out and tell me if it works for them?

    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)
    		{
    			String fileName = f.getName();
    			int extIndex = fileName.lastIndexOf(".");
    			String ext = fileName.substring(extIndex+1);
    			
    			while (ext.equalsIgnoreCase("zip"))
    			{
    				try
    				{
    					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())
    						{
    							String entryName = zipEntry.getName();
    							if (entryName.endsWith(".jpeg"))
    							{
    								InputStream entryStream = zipFile.getInputStream(zipEntry);
    								Image image = ImageIO.read(entryStream);
    								File outputfile = new File("C:/Documents and Settings/" + entryName);
    								ImageIO.write((RenderedImage) image, "jpeg", outputfile);
    								JOptionPane.showMessageDialog(null, new ImageIcon(image));
    							}
    						}
    					}
    				}
    				catch (ZipException e)
    				{
    					e.printStackTrace();
    				} 
    				catch (IOException e)
    				{
    					e.printStackTrace();
    				}
    			}
    		}	
    	}
    }

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

    Default

    Quote Originally Posted by jing-yi View Post
    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).
    Yes, that's exactly what I want to do. I basically just want to ignore everything that is not a ".jpeg" file.

    And I'm sorry, but I'm not sure what exactly you mean by 'file skeleton', can you re-phrase that?

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

    Default

    Java Code:
    while (!zipEntry.isDirectory())
    I see no code which allows you to break out of this loop - change to an if conditional. It helps when something like this happens to litter your code with System.out.println statements, which lets you know what parts of the code are being executed, when, and how many times.

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

    Default

    K, I just changed that to an if conditional. I also took your advice and littered my code with println statements. Now when I run it it is basically jumping between "Here3" and "Here4". I'm not sure if this is an infinite loop (most likely) or if the files are just that extensive to look through?

    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)
    		{
    			String fileName = f.getName();
    			int extIndex = fileName.lastIndexOf(".");
    			String ext = fileName.substring(extIndex+1);
    			
    			while (ext.equalsIgnoreCase("zip"))
    			{
    				System.out.println("Here1");
    				try
    				{
    					System.out.println("Here2");
    					ZipFile zipFile = new ZipFile("C:/Documents and Settings/" + fileName);
    					for (Enumeration e = zipFile.entries(); e.hasMoreElements();) 
    					{
    						System.out.println("Here3");
    						ZipEntry zipEntry = zipFile.entries().nextElement();;
    						//while the entry is not a directory
    						if (!zipEntry.isDirectory())
    						{
    							System.out.println("Here4");
    							String entryName = zipEntry.getName();
    							if (entryName.endsWith(".jpeg"))
    							{
    								System.out.println("Here5");
    								InputStream entryStream = zipFile.getInputStream(zipEntry);
    								Image image = ImageIO.read(entryStream);
    								File outputfile = new File("C:/Documents and Settings/" + entryName);
    								ImageIO.write((RenderedImage) image, "jpeg", outputfile);
    								JOptionPane.showMessageDialog(null, new ImageIcon(image));
    							}
    						}
    					}
    				}
    				catch (ZipException e)
    				{
    					e.printStackTrace();
    				} 
    				catch (IOException e)
    				{
    					e.printStackTrace();
    				}
    				System.out.println("Here6");
    			}
    			System.out.println("Here7");
    		}	
    	}
    }
    Last edited by JavaN00bie; 06-16-2011 at 08:24 PM.

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

    Default

    Just realized a few things thanks to you guys:

    1) There is indeed an infinite loop in there still
    2) When I try to access just a zip folder containing nothing but images, the program does work (sort of)...however it only is able to re-open to same image over and over.
    3) My current code may not be able to access the "/word/media" path in the zip file archives to extract the necessary images.

    So look like it is back to the drawing board, kind of, where to start this time though...lol

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

    Default

    Those println's should help you step through the logic of the code. To address your concerns
    1) Step through the code incrementally as the computer would - in particular check your use of while in your code, which if unbroken is a recipe for an infinite loop.
    2) Look at how you are enumerating the list of files in the zip - on a quick look it appears you will continually get the Enumerator and read the first in said enumerator
    3) There are relatively easy recursive solutions to traverse a directory structure (google might help you here)

    As I mentioned a while back, you need to break the problem down. First, read the zip...its obvious this step has not been completed so you should not continue to the next steps of reading the files until you've solved this requirement.

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

    Default

    See what I don't get (as it seems logically correct to me) is why when it gets to:

    ZipFile zipFile = new ZipFile("C:/Documents and Settings/rajeeva/My Documents/JavaTest/Test/" + fileName);

    the second time, an error is produced?

    Exception in thread "main" java.util.zip.ZipException: error in opening zip file
    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 ThisIsTest.main(ThisIsTest.java:29)
    Shouldn't it be iterating to the next file by that point?

  9. #29
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

    Default

    Shouldn't it be iterating to the next file by that point?
    No...read my feedback from above, and look closely at your code to should see why (and be sure to close() the zip file as well).

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

    Default

    So I've made SOME progress.

    Java Code:
    import java.awt.Image;
    import java.awt.image.BufferedImage;
    import java.awt.image.RenderedImage;
    import java.io.File;
    import java.io.FileInputStream;
    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 java.util.zip.ZipInputStream;
    
    import javax.imageio.ImageIO;
    import javax.swing.ImageIcon;
    import javax.swing.JOptionPane;
    
    import java.io.File;
    
    public class ThisIsTest 
    {
    	public static void main(String[] args) throws IOException
    	{
    		File zipDirectory = new File("C:/Documents and Settings/");
    		File files2[] = zipDirectory.listFiles();
    		
    		for (File f : files2)
    		{
    			System.out.println("First");
    			int i = 0;
    			if (f.exists() == true)
    			{
    				try
    				{
    					String fileName = f.getName();
    					ZipFile zipFile = new ZipFile("C:/Documents and Settings/" + fileName);
    					String zipName = zipFile.getName();
    					FileInputStream inStream = new FileInputStream(zipName);
    					ZipInputStream zis = new ZipInputStream(inStream);
    					ZipEntry zipEntry;
    					System.out.println("Second");
    					while ((zipEntry = zis.getNextEntry()) != null)
    					{
    						System.out.println("Third");
    						String entryName  = zipEntry.getName();
    						boolean ext = entryName.endsWith(".jpeg");
    						if (ext == true)
    						{
    							System.out.println("Fourth");
    							InputStream entryStream = zipFile.getInputStream(zipEntry);
    							Image image = ImageIO.read(entryStream);
    							File outputfile = new File("C:/Documents and Settings/" + entryName);
    							ImageIO.write((RenderedImage) image, "jpeg", outputfile);
    							System.out.println(i++);
    						}
    						System.out.println("Fifth");
    					}
    					System.out.println("Sixth");
    				}		
    				catch (ZipException e)
    				{
    					e.printStackTrace();
    				} 
    				catch (IOException e)
    				{
    					e.printStackTrace();				
    				}
    			}
    			else
    			{
    				break;
    			}
    			System.out.println("Seventh");
    		}
    	}
    }
    Now this code pulls out all the images from a directory of zip folders containing nothing BUT images. However, when you try to extract images from an archive containing other directories, other files (.xml, .txt, etc) I am bombarded with errors.

    java.util.zip.ZipException: error in opening zip file
    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 ThisIsTest.main(ThisIsTest.java:36)
    java.io.FileNotFoundException: C:\Documents and Settings\word\media\image20.jpeg (The system cannot find the path specified)
    at java.io.RandomAccessFile.open(Native Method)
    at java.io.RandomAccessFile.<init>(Unknown Source)
    at javax.imageio.stream.FileImageOutputStream.<init>( Unknown Source)
    at com.sun.imageio.spi.FileImageOutputStreamSpi.creat eOutputStreamInstance(Unknown Source)
    at javax.imageio.ImageIO.createImageOutputStream(Unkn own Source)
    at javax.imageio.ImageIO.write(Unknown Source)
    at ThisIsTest.main(ThisIsTest.java:53)
    Exception in thread "main" java.lang.NullPointerException
    at javax.imageio.ImageIO.write(Unknown Source)
    at ThisIsTest.main(ThisIsTest.java:53)
    And I've looked around Google, but I have absolutely no idea how to traverse a zip archive directory structure. Any ideas?

    I thought it would be something like "C:/Documents and Settings/test.zip/word/media" for the folder where the images are stored, and "C:/Documents and Settings/test.zip/word/media/image1.jpeg" to locate the individual image, but I guess not?

    Edit: Just to add, I made some minor revisions here and now I am getting an error at the end of the code...but it's going through the code like it should be, it is just not extracting the images like I want. :/

    Java Code:
    import java.awt.Image;
    import java.awt.image.BufferedImage;
    import java.awt.image.RenderedImage;
    import java.io.File;
    import java.io.FileInputStream;
    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 java.util.zip.ZipInputStream;
    
    import javax.imageio.ImageIO;
    import javax.swing.ImageIcon;
    import javax.swing.JOptionPane;
    
    import java.io.File;
    
    public class ThisIsTest 
    {
    	public static void main(String[] args) throws IOException
    	{
    		File zipDirectory = new File("C:/Documents and Settings/");
    		File files2[] = zipDirectory.listFiles();
    		
    		for (File f : files2)
    		{
    			System.out.println("First");
    			int i = 0;
    			if (f.exists() == true)
    			{
    				try
    				{
    					String fileName = f.getName();
    					ZipFile zipFile = new ZipFile("C:/Documents and Settings/" + fileName);
    					String zipName = zipFile.getName();
    					FileInputStream inStream = new FileInputStream(zipName);
    					ZipInputStream zis = new ZipInputStream(inStream);
    					ZipEntry zipEntry;
    					System.out.println("Second");
    					while ((zipEntry = zis.getNextEntry()) != null)
    					{
    						System.out.println("Third");
    						if (!zipEntry.isDirectory())
    						{
    							String entryName  = zipEntry.getName();
    							boolean ext = entryName.endsWith(".jpeg");
    							if (ext == true)
    							{
    								System.out.println("Fourth");
    								InputStream entryStream = zipFile.getInputStream(zipEntry);
    								Image image = ImageIO.read(entryStream);
    								File outputfile = new File("C:/Documents and Settings/" + entryName.lastIndexOf("/") + ".jpeg");
    								ImageIO.write((RenderedImage) image, "jpeg", outputfile);
    								System.out.println(i++);
    							}
    							System.out.println("Fifth");
    						}
    					}
    					System.out.println("Sixth");
    				}		
    				catch (ZipException e)
    				{
    					e.printStackTrace();
    				} 
    				catch (IOException e)
    				{
    					e.printStackTrace();				
    				}
    			}
    			else
    			{
    				break;
    			}
    			System.out.println("Seventh");
    		}
    	}
    }
    And the weird thing about this code, I used it on one zip file located in that directory that contained only the directory "/word/media/" which contained about 30 ".jpeg" images all titled "imageN.jpeg" (where N is any number from 1-30). The program extracted one image and called it "10.jpeg", however it was not the same "image10.jpeg" located in the zip archive. No clue what that means.
    Last edited by JavaN00bie; 06-17-2011 at 05:33 PM.

Page 2 of 2 FirstFirst 12

Similar Threads

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