• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

After Unzip delete that file programatically??

 
Ranch Hand
Posts: 342
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 342
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Thanks for ur suggestion.
Thanks & Regards
Bikash
 
The human mind is a dangerous plaything. This tiny ad is pretty safe:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic