wood burning stoves*
The moose likes Java in General and the fly likes After Unzip delete that file programatically?? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "After Unzip delete that file programatically??" Watch "After Unzip delete that file programatically??" New topic
Author

After Unzip delete that file programatically??

Bikash Paul
Ranch Hand

Joined: Dec 04, 2001
Posts: 342
Hi all friends,
I want to delete that .zip file which I have unziped progrmatically with my below codes.With my below codes I couldn't delete that .zip file after unzip.Can any one plz test my below codes where Iam wrong.Eagerly waiting for someone reply.
import java.awt.*;
import java.io.*;
import java.util.*;
import java.util.zip.*;
import javax.swing.*;
public class Unzipper {
public static ProgressMonitor monitor;
public static Final file;
public Unzipper() {}
public static byte[] buffer = new byte[16384];
public static void unzip(File file, Component parentComponent, File outputDir) throws ZipException, IOException {
unzip(new ZipFile(file), parentComponent, outputDir);
}
public static void unzip(File file, int mode, Component parentComponent, File outputDir) throws IOException {
unzip(new ZipFile(file, mode), parentComponent, outputDir);
}
public static void unzip(String name, Component parentComponent, File outputDir) throws IOException {
unzip(new ZipFile(name), parentComponent, outputDir);
}
public static void unzip(ZipFile zip, Component parentComponent, File outputDir) throws IOException {
if ( zip == null) {
throw new NullPointerException("outputDir is null");
}
int size = zip.size();
if (size > 0) {
if (outputDir == null) {
throw new NullPointerException("outputDir is null");
}
if (!outputDir.exists()) {
outputDir.mkdirs();
}
monitor = new ProgressMonitor(parentComponent,"Unpacking " + zip.getName() + "...", "", 0, size);
monitor.setMillisToDecideToPopup(0);
monitor.setMillisToPopup(0);
Enumeration entries = zip.entries();
for ( int i = 0; entries.hasMoreElements(); ) {
ZipEntry entry = (ZipEntry) entries.nextElement();
monitor.setNote(entry.getName());
file = new File(outputDir, entry.getName());
file.getParentFile().mkdirs();
if (!entry.isDirectory()) {
InputStream in = null;
OutputStream out = null;
try {
in = zip.getInputStream(entry);
out = new FileOutputStream(file);
for (int n=0; (n = in.read(buffer)) >= 0; )
out.write(buffer, 0, n);
monitor.setProgress(++i);
out.flush();
out.close();
in.close();
}catch(IOException ioex){
ioex.printStackTrace();
}
}
}
}
}

public static void main(String[] args) throws ZipException, IOException
{
if (args.length !=2) {
System.err.println("Usage: java Unzipper <zip file> <output dir>");
System.exit(1);
}
long start = -System.currentTimeMillis();
unzip(args[0], null, new File(args[1]));
monitor.close();
file.delete();
System.out.println(start+System.currentTimeMillis());
}
}
Regards
Bikash
[ July 10, 2003: Message edited by: Bikash Paul ]
Pete Harris
Ranch Hand

Joined: Feb 05, 2003
Posts: 39
Bikash,
the problem here is that you've got a static variable 'file' which is not properly defined. What you need to do is make it a local variable created inside the zip enumeration, and change your main() method so that it defines it's own file variable. You should also close the zip file just before the end of the unzip() method.
I've run a quick test with these changes and it seems to work fine,
cheers, Pete
Bikash Paul
Ranch Hand

Joined: Dec 04, 2001
Posts: 342
Hi,
Thanks for ur suggestion.
Thanks & Regards
Bikash
 
Consider Paul's rocket mass heater.
 
subject: After Unzip delete that file programatically??
 
Similar Threads
UnZip error
Problem unzipping with ZipInputStream, containing filename with Swedish characters
UnZipping Occurs in Server System only
Unzip problem .. please its urgent
Servlet Program doesnt work in Client Side.