Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Java: Copy file -> Zipped it -> FTP it : How to do it? 1

Status
Not open for further replies.

offhegoes

Programmer
Jul 20, 2003
5
MY
Hi Everyone,

I need to write a program that will copy files (Files & Folders) from a shared directory on another computer (my platform is Windows) from my computer. Then, after copying is done, I need to zipped it. Once zipping process is done, I need to FTP it.

Ok, on the FTP-ing the files is OK & tested succesfully. But, on the earlier steps, I need some suggestion. I don't have much experience on Java, I am more on C, C++ & Perl, PHP programmer.

If you can give some guide that will be good.

Thanks,
M Sopian H
 
A very basic example to put some files in a zipfile. It only demonstrates the use of ZipOutputStream. You probably want to zip a complete directory.

package com.ecc.swing2;

import java.io.*;
import java.util.zip.*;

public class ZipExample {

public ZipExample() {
}

public void doIt() {
String[] filenames = new String[]{"D:\\Testdir\\file1.txt", "D:\\Testdir\\file2.txt"};
byte[] buf = new byte[1024];

try {
String outFilename = "D:\\TestDir\\outfile.zip";
ZipOutputStream out = new ZipOutputStream(new FileOutputStream(outFilename));

for (int i=0; i<filenames.length; i++) {
FileInputStream in = new FileInputStream(filenames);
out.putNextEntry(new ZipEntry(filenames));
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
out.closeEntry();
in.close();
}
out.close();
} catch (IOException e) {
}
}

public static void main(String args[]) {
ZipExample zipExample = new ZipExample();
zipExample.doIt();
}

}
 
I have tried to run your code, but, got error.. and I don't know what does it mean.

------------
D:\Software-Installer\Java>javac ZipExample.java
ZipExample.java:20: cannot resolve symbol
symbol : constructor FileInputStream (java.lang.String[])
location: class java.io.FileInputStream
FileInputStream in = new FileInputStream(filenames);
^
ZipExample.java:21: cannot resolve symbol
symbol : constructor ZipEntry (java.lang.String[])
location: class java.util.zip.ZipEntry
out.putNextEntry(new ZipEntry(filenames));
^
2 errors

D:\Software-Installer\Java>echo %CLASSPATH%
D:\Program Files\JAVASDK2\demo\jfc\SimpleExample\src;D:\Program Files\JAVASDK2\l
ib\comm.jar;d:\Nuance\v8.0.0\java\nsc.jar;d:\Nuance\v8.0.0\java\vcomsc.jar;d:\Nu
ance\v8.0.0\java\swingall.jar;D:\j2sdk1.4.0\bin

D:\Software-Installer\Java>
 
Sorry, first one wrongly display the error pointer, it supposed to point at the 'new' word. So, I repost and adjust it.
----------
D:\Software-Installer\Java>javac ZipExample.java
ZipExample.java:20: cannot resolve symbol
symbol : constructor FileInputStream (java.lang.String[])
location: class java.io.FileInputStream
FileInputStream in = new FileInputStream(filenames);
^
ZipExample.java:21: cannot resolve symbol
symbol : constructor ZipEntry (java.lang.String[])
location: class java.util.zip.ZipEntry
out.putNextEntry(new ZipEntry(filenames));
^
2 errors

D:\Software-Installer\Java>echo %CLASSPATH%
D:\Program Files\JAVASDK2\demo\jfc\SimpleExample\src;D:\Program Files\JAVASDK2\l
ib\comm.jar;d:\Nuance\v8.0.0\java\nsc.jar;d:\Nuance\v8.0.0\java\vcomsc.jar;d:\Nu
ance\v8.0.0\java\swingall.jar;D:\j2sdk1.4.0\bin

D:\Software-Installer\Java>
 
A more complete example that zips a complete directory.
PS : Do you need to copy the files first from the shared directory? Can't you just zip them from the shared directory?
PS : No error checking is done. Hidden files are also zipped.

package com.ecc.swing2;

import java.io.*;
import java.util.zip.*;

public class ZipExample {

private ZipOutputStream out;
private byte[] buf;

public ZipExample() {
}

private void doIt() {
String inDirName = &quot;D:\\Testdir&quot;;
String outFileName = &quot;D:\\OutDir\\outfile.zip&quot;;
zip(inDirName, outFileName);
}

private void zip(String inDirName, String outFileName) {
buf = new byte[1024];
try {
out = new ZipOutputStream(new FileOutputStream(outFileName));
File dir = new File(inDirName);
zipDirectory(dir);
out.close();
} catch (IOException e) {
}
}

private void zipDirectory(File dir) {
if (dir!=null) {
if (dir.isDirectory()) {
//File dir = new File(nodeInfo.path);
//selectedDiskObjectName = dir.getAbsolutePath();
//System.out.println(&quot;selected from getChil:&quot; + selectedDiskObjectName);
File[] files = dir.listFiles();
int fileCount = files.length;
for (int i=0; i<fileCount; i++) {
File file = files;
if (file.isDirectory()) {
//File[] children = file.getChildren(file);
zipDirectory(file);
}
else {
String fileName = file.getAbsolutePath();
System.out.println(&quot;Zipping file &quot; + fileName);
zipFile(fileName);
}
}
}
}
}

public void zipFile(String file) {
try {
FileInputStream in = new FileInputStream(file);
out.putNextEntry(new ZipEntry(file));
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
out.closeEntry();
in.close();
} catch (IOException e) {
}
}

public static void main(String args[]) {
ZipExample zipExample = new ZipExample();
zipExample.doIt();
}

}
 
Sorry, after the parameter filenames there should be an &quot;open square bracket&quot; an &quot;i&quot; and a &quot;closing square bracket&quot;. But it was being interpreted by this site as &quot;put in italics&quot;. I forgot to put code /code around it.

Code:
  FileInputStream in = new FileInputStream(filenames[i]);
  out.putNextEntry(new ZipEntry(filenames[i]));
 
Hopefully the final version :

Code:
package com.ecc.swing2;

import java.io.*;
import java.util.zip.*;

public class ZipExample {

  private ZipOutputStream out;
  private byte[] buf;

  public ZipExample() {
  }

  private void doIt() {
    String inDirName = &quot;D:\\Testdir&quot;;
    String outFileName = &quot;D:\\OutDir\\outfile.zip&quot;;
    zip(inDirName, outFileName);
  }

  private void zip(String inDirName, String outFileName) {
    buf = new byte[1024];
    try {
        out = new ZipOutputStream(new FileOutputStream(outFileName));
        File dir = new File(inDirName);
        zipDirectory(dir);
        out.close();
    } catch (IOException e) {
    }
  }

  private void zipDirectory(File dir) {
    if (dir!=null) {
      if (dir.isDirectory()) {
        //File dir = new File(nodeInfo.path);
        //selectedDiskObjectName = dir.getAbsolutePath();
        //System.out.println(&quot;selected from getChil:&quot; + selectedDiskObjectName);
        File[] files = dir.listFiles();
        int fileCount = files.length;
        for (int i=0; i<fileCount; i++) {
          File file = files[i];
          if (file.isDirectory()) {
            //File[] children = file.getChildren(file);
            zipDirectory(file);
          }
          else {
            String fileName = file.getAbsolutePath();
            System.out.println(&quot;Zipping file &quot; + fileName);
            zipFile(fileName);
          }
        }
      }
    }
  }

  public void zipFile(String file) {
    try {
      FileInputStream in = new FileInputStream(file);
      out.putNextEntry(new ZipEntry(file));
      int len;
      while ((len = in.read(buf)) > 0) {
        out.write(buf, 0, len);
      }
      out.closeEntry();
      in.close();
    } catch (IOException e) {
    }
  }

  public static void main(String args[]) {
    ZipExample zipExample = new ZipExample();
    zipExample.doIt();
  }

}
 
Sorry, I just do not get my feet firmly-stand to understand Java yet. Anyway I got your program compiled with Javac command, but, It still does not run when I run java command. Below are the error that I get, I think it may be just the configuration.
-----------------------

D:\Software-Installer\Java>set CLASSPATH=%CLASSPATH%;D:\Software-Installer\Java

D:\Software-Installer\Java>java ZipExample
Exception in thread &quot;main&quot; java.lang.NoClassDefFoundError: ZipExample (wrong nam
e: com/ecc/swing2/ZipExample)
at java.lang.ClassLoader.defineClass0(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:509)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:12
3)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:246)
at java.net.URLClassLoader.access$100(URLClassLoader.java:54)
at java.net.URLClassLoader$1.run(URLClassLoader.java:193)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:186)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:265)
at java.lang.ClassLoader.loadClass(ClassLoader.java:262)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:322)

D:\Software-Installer\Java>

------------
Thanks,
M Sopian H
 
Remove the line

Code:
package com.ecc.swing2;

recompile and try again.

 
Or leave the source like it is, but move the class file to subdirectory &quot;D:\Software-Installer\Java\com\ecc\swing2\&quot; and execute as :

D:\Software-Installer\Java>java com.ecc.swing2.ZipExample
 
Hey,

Thanks, it's work... I just to remove the link.

It just great here, this is my first time using this place and I have a very good support. Thanks again.

M Sopian H
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top