By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,296 Members | 1,457 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,296 IT Pros & Developers. It's quick & easy.

Problem in downloading

P: 6
Hi friends,

I have a servlet coding where i can download a folder from FTP.
The folder is zipped and stored in the FTP server.
While the client downloads the zip file the it should be unzipped and stored in the client machine.
Here is the code..


Expand|Select|Wrap|Line Numbers
  1. import javax.servlet.http.*;
  2. import javax.servlet.*;
  3. import javax.servlet.ServletOutputStream;
  4. import java.io.*;
  5. import java.awt.*;
  6. import java.util.*;
  7. import java.util.zip.*;
  8. import java.net.*;
  9.  
  10. public class TempDownload1 extends HttpServlet{
  11.  
  12. public static void unzip(ZipFile zip, Component parentComponent, File outputDir) throws IOException
  13. {}
  14.  
  15. public void doGet(HttpServletRequest req, HttpServletResponse res)
  16.  throws ServletException, IOException
  17.  {
  18.     byte[] buffer = new byte[16384];
  19.     //source
  20.     ZipFile zip=new ZipFile("ftp://username:pswd@xxx.xx.xxx.xx/location/filename.zip");
  21.     Component parentComponent=null;
  22.     //destination
  23.     File outputDir = new File("C:/");
  24.     Enumeration entries = zip.entries();
  25.     for ( int i = 0; entries.hasMoreElements(); )
  26.             {
  27.     ZipEntry entry = (ZipEntry) entries.nextElement();
  28.     File File = new File(outputDir, entry.getName());
  29.     File.getParentFile().mkdirs();
  30.     if (!entry.isDirectory())
  31.       {
  32.         InputStream in = null;
  33.         OutputStream out = null;
  34.     try {
  35.          in = zip.getInputStream(entry);
  36.          out = new FileOutputStream(File);
  37.          for (int n = 0; (n = in.read(buffer)) > -1; )
  38.          out.write(buffer, 0, n);
  39.          }
  40.                  finally {
  41.         try {
  42.                out.close();
  43.                          }
  44.         catch (IOException e) {}
  45.                }
  46.         }
  47.      }
  48. unzip(zip, parentComponent ,outputDir);
  49.  
  50.     }
  51. }
While trying to compile the servlet code it's fine but when the servlet is executed the following exception occurs

Exception : java.util.zip.ZipException: The filename, directory name, or volume label syntax is incorrect.

Can anyone help me out..
Thanks in advance
Sep 7 '07 #1
Share this Question
Share on Google+
10 Replies


10K+
P: 13,264
Hi friends,

I have a servlet coding where i can download a folder from FTP.
The folder is zipped and stored in the FTP server.
While the client downloads the zip file the it should be unzipped and stored in the client machine.
Here is the code..


Expand|Select|Wrap|Line Numbers
  1. import javax.servlet.http.*;
  2. import javax.servlet.*;
  3. import javax.servlet.ServletOutputStream;
  4. import java.io.*;
  5. import java.awt.*;
  6. import java.util.*;
  7. import java.util.zip.*;
  8. import java.net.*;
  9.  
  10. public class TempDownload1 extends HttpServlet{
  11.  
  12. public static void unzip(ZipFile zip, Component parentComponent, File outputDir) throws IOException
  13. {}
  14.  
  15. public void doGet(HttpServletRequest req, HttpServletResponse res)
  16.  throws ServletException, IOException
  17.  {
  18.     byte[] buffer = new byte[16384];
  19.     //source
  20.     ZipFile zip=new ZipFile("ftp://username:pswd@xxx.xx.xxx.xx/location/filename.zip");
  21.     Component parentComponent=null;
  22.     //destination
  23.     File outputDir = new File("C:/");
  24.     Enumeration entries = zip.entries();
  25.     for ( int i = 0; entries.hasMoreElements(); )
  26.             {
  27.     ZipEntry entry = (ZipEntry) entries.nextElement();
  28.     File File = new File(outputDir, entry.getName());
  29.     File.getParentFile().mkdirs();
  30.     if (!entry.isDirectory())
  31.       {
  32.         InputStream in = null;
  33.         OutputStream out = null;
  34.     try {
  35.          in = zip.getInputStream(entry);
  36.          out = new FileOutputStream(File);
  37.          for (int n = 0; (n = in.read(buffer)) > -1; )
  38.          out.write(buffer, 0, n);
  39.          }
  40.                  finally {
  41.         try {
  42.                out.close();
  43.                          }
  44.         catch (IOException e) {}
  45.                }
  46.         }
  47.      }
  48. unzip(zip, parentComponent ,outputDir);
  49.  
  50.     }
  51. }

While trying to compile the servlet code it's fine but when the servlet is executed the following exception occurs

Exception : java.util.zip.ZipException: The filename, directory name, or volume label syntax is incorrect.

Can anyone help me out..
Thanks in advance
At which line?
Sep 7 '07 #2

P: 6
At which line?

At line num 20
i.e., ZipFile zip=new ZipFile("ftp://username:pswd@xxx.xx.xxx.xx/location");
Sep 7 '07 #3

Nepomuk
Expert 2.5K+
P: 3,112
Hi friends,

I have a servlet coding where i can download a folder from FTP.
The folder is zipped and stored in the FTP server.
While the client downloads the zip file the it should be unzipped and stored in the client machine.
Here is the code..


Expand|Select|Wrap|Line Numbers
  1. import javax.servlet.http.*;
  2. import javax.servlet.*;
  3. import javax.servlet.ServletOutputStream;
  4. import java.io.*;
  5. import java.awt.*;
  6. import java.util.*;
  7. import java.util.zip.*;
  8. import java.net.*;
  9.  
  10. public class TempDownload1 extends HttpServlet{
  11.  
  12. public static void unzip(ZipFile zip, Component parentComponent, File outputDir) throws IOException
  13. {}
  14.  
  15. public void doGet(HttpServletRequest req, HttpServletResponse res)
  16.  throws ServletException, IOException
  17.  {
  18.     byte[] buffer = new byte[16384];
  19.     //source
  20.     ZipFile zip=new ZipFile("ftp://username:pswd@xxx.xx.xxx.xx/location/filename.zip");
  21.     Component parentComponent=null;
  22.     //destination
  23.     File outputDir = new File("C:/");
  24.     Enumeration entries = zip.entries();
  25.     for ( int i = 0; entries.hasMoreElements(); )
  26.             {
  27.     ZipEntry entry = (ZipEntry) entries.nextElement();
  28.     File File = new File(outputDir, entry.getName());
  29.     File.getParentFile().mkdirs();
  30.     if (!entry.isDirectory())
  31.       {
  32.         InputStream in = null;
  33.         OutputStream out = null;
  34.     try {
  35.          in = zip.getInputStream(entry);
  36.          out = new FileOutputStream(File);
  37.          for (int n = 0; (n = in.read(buffer)) > -1; )
  38.          out.write(buffer, 0, n);
  39.          }
  40.                  finally {
  41.         try {
  42.                out.close();
  43.                          }
  44.         catch (IOException e) {}
  45.                }
  46.         }
  47.      }
  48. unzip(zip, parentComponent ,outputDir);
  49.  
  50.     }
  51. }
While trying to compile the servlet code it's fine but when the servlet is executed the following exception occurs

Exception : java.util.zip.ZipException: The filename, directory name, or volume label syntax is incorrect.

Can anyone help me out..
Thanks in advance
IDoes ZipFile really support ftp adresses? I doubt it. "File" doesn't seem to support them.

Can't you give a relative adress? If it's the same filesystem, it won't be so problematic. This way, it tries to connect to
C:\workspace\ftp:\username:pswd@xxx.xx.xxx.xx\loca tion\filename.zip (when "C:\workspace" is your working directory), which is of course not what you're looking for.

Greetings,
Nepomuk
Sep 7 '07 #4

10K+
P: 13,264
At line num 20
i.e., ZipFile zip=new ZipFile("ftp://username:pswd@xxx.xx.xxx.xx/location");
Why don't use a ZipInputStream as explained here.
Sep 7 '07 #5

P: 6
Why don't use a ZipInputStream as explained here.


I tried with the ZipInputStream also. Still the same exception occurs
Sep 7 '07 #6

Nepomuk
Expert 2.5K+
P: 3,112
I tried with the ZipInputStream also. Still the same exception occurs
Is it an external file you're accessing? If so, I guess, you'd have to download the class to your server, unzip it there and then send it to the client. It's the same, when you want to open a file from the web on your computer - you have to download it first.
If the file is local, access it in a local way. That's much easier than all of this ftp stuff.

Greetings,
Nepomuk
Sep 7 '07 #7

P: 6
Is it an external file you're accessing? If so, I guess, you'd have to download the class to your server, unzip it there and then send it to the client. It's the same, when you want to open a file from the web on your computer - you have to download it first.
If the file is local, access it in a local way. That's much easier than all of this ftp stuff.

Greetings,
Nepomuk



Yes it's an external file that i am trying to access.
When i use it for a local file it works perfectly


Eg: ZipFile zip=new ZipFile("C:/filename.zip");

But it is not working for the FTP

So i tried using this way

URL zip=new URL("ftp://username:pswd@xxx.xx.xxx.xx/location/filename.zip");

But while trying to use this way it is not supporting the 'Enumeration' and the getInputStream() method
Sep 7 '07 #8

Nepomuk
Expert 2.5K+
P: 3,112
Yes it's an external file that i am trying to access.
When i use it for a local file it works perfectly

Eg: ZipFile zip=new ZipFile("C:/filename.zip");

But it is not working for the FTP

So i tried using this way

URL zip=new URL("ftp://username:pswd@xxx.xx.xxx.xx/location/filename.zip");

But while trying to use this way it is not supporting the 'Enumeration' and the getInputStream() method
OK, seems you'll have to download it first. If you don't have ftp classes in your project already, have a look at the commons-net lib from Apache (or, if you choose to, any other ftp lib). With this, you can download your external file and as soon as it's local, you have the solution!

Greetings,
Nepomuk
Sep 7 '07 #9

P: 6
OK, seems you'll have to download it first. If you don't have ftp classes in your project already, have a look at the commons-net lib from Apache (or, if you choose to, any other ftp lib). With this, you can download your external file and as soon as it's local, you have the solution!

Greetings,
Nepomuk

I am not able to download the file even after i tried to use that.
The problem is with the URL method..
Is there any other way to alter the code to make it perfect
Sep 7 '07 #10

10K+
P: 13,264
I am not able to download the file even after i tried to use that.
The problem is with the URL method..
Is there any other way to alter the code to make it perfect
Perhaps you should go through a tutorial on how to copy a file over the network
Sep 7 '07 #11

Post your reply

Sign in to post your reply or Sign up for a free account.