I'm study Compressing and Decompressing Data Using Java APIs from
http://java.sun.com/developer/technicalArticles/Programming/compression/
but when i compile this example:

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

public class UnZip {
   final int BUFFER = 2048;
   public static void main (String[] args) {
      try {
         String fileName = "your zip file" //but your zip file 
         BufferedOutputStream dest = null;
         FileInputStream fis = new 
	   FileInputStream(fileName);
         ZipInputStream zis = new 
	   ZipInputStream(new BufferedInputStream(fis));
         ZipEntry entry;
         while((entry = zis.getNextEntry()) != null) {
            System.out.println("Extracting: " +entry);
            int count;
            byte data[] = new byte[BUFFER];
            // write the files to the disk
            FileOutputStream fos = new 
	      FileOutputStream(entry.getName());
            dest = new 
              BufferedOutputStream(fos, BUFFER);
            while ((count = zis.read(data, 0, BUFFER)) 
              != -1) {
               dest.write(data, 0, count);
            }
            dest.flush();
            dest.close();
         }
         zis.close();
      } catch(Exception e) {
         e.printStackTrace();
      }
   }
}

that's do nothing , where is the fault ?

Recommended Answers

All 14 Replies

is an exception message thrown? is there no zip file created?
how do you know there is a fault?

is an exception message thrown? is there no zip file created?
how do you know there is a fault?

yes there is no exception message or any thing happened , and sry i mean my fault

On line 8 did you put the name of a real zip file?

On line 8 did you put the name of a real zip file?

of course yes :)

of course yes :)

I think i found the problem:

FileOutputStream fos = new 
              FileOutputStream(entry.getName());

this extratcs all the data to your jars current directory??? try this:

FileOutputStream fos = new 
              FileOutputStream("c:\\"+entry.getName());

btw your buffer should be static, and your missing ';' after zip name:

final int BUFFER....//change to static
            String fileName = "...." //put a semicolon at the end

I think i found the problem:

FileOutputStream fos = new 
              FileOutputStream(entry.getName());

this extratcs all the data to your jars current directory??? try this:

FileOutputStream fos = new 
              FileOutputStream("c:\\"+entry.getName());

btw your buffer should be static, and your missing ';' after zip name:

final int BUFFER....//change to static
            String fileName = "...." //put a semicolon at the end

I'm try it now but there is nothing change

I'm try it now but there is nothing change

I dont understand?

Presumably, if System.out.println("Extracting: " +entry); isn't outputting anything, AND there isn't a java.io.FileNotFoundException then the input file has been found, but it doesn't contain any zipped content?

Here's a really dumb question... is it possible that the code your are executing isn't what you think - eg out-of-date .class files hanging around etc? Maybe a quick print at the start and end of execution would give positive confirmation?

I dont understand?

me to , try this code and you will get same results

Presumably, if System.out.println("Extracting: " +entry); isn't outputting anything, AND there isn't a java.io.FileNotFoundException then the input file has been found, but it doesn't contain any zipped content?

Maybe what JamesCherrill is right.. Go to your c drive, create two text files with anynames and any random data... next select both textfiles, right click and send to Compressed zip file... next name that compressed zip file test.zip and make sure it resides in your c:\\test.zip... now try running this and seeing what happens...(delete the files after you zipped them or you might get confused as to whether they were extracted or the originals) now try here:

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

public class Unzip {

    static final int BUFFER = 2048;

    public static void main(String[] args) {
        try {
            String fileName = "c:\\test.zip"; //but your zip file 
            String unzipToFolder = "c:\\";
            BufferedOutputStream dest = null;
            FileInputStream fis = new FileInputStream(fileName);
            ZipInputStream zis = new ZipInputStream(new BufferedInputStream(fis));
            ZipEntry entry;
            while ((entry = zis.getNextEntry()) != null) {
                System.out.println("Extracting: " + entry);
                int count;
                byte data[] = new byte[BUFFER];
                // write the files to the disk
                FileOutputStream fos = new FileOutputStream(unzipToFolder + entry.getName());
                dest = new BufferedOutputStream(fos, BUFFER);
                while ((count = zis.read(data, 0, BUFFER))
                        != -1) {
                    dest.write(data, 0, count);
                }
                dest.flush();
                dest.close();
            }
            zis.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

[edit]obviously i dont get the same results or else i wouldnt have said i found the problem...And yes this code is tested and works

Presumably, if System.out.println("Extracting: " +entry); isn't outputting anything, AND there isn't a java.io.FileNotFoundException then the input file has been found, but it doesn't contain any zipped content?

no that's contains a ziped files

I edited that down to the essential code to see into the zip file, and used a file input dialog to ensure the zip file path was correct. This code works for me - maybe you can try it with your zip file?

void doZip() {

      try {
         JFileChooser fileChooser = new JFileChooser();
         fileChooser.showOpenDialog(null);
         File chosen = fileChooser.getSelectedFile();

         FileInputStream fis = new FileInputStream(chosen);
         ZipInputStream zis = new ZipInputStream(new BufferedInputStream(fis));
         ZipEntry entry;
         while ((entry = zis.getNextEntry()) != null) {
            System.out.println("Found: " + entry);
         }
         zis.close();
      } catch (Exception e) {
         e.printStackTrace();
      }
   }

I'm know now why it's no work , I'm do security on my partions thank you for help all

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.