Solved

Unzip question

Posted on 2006-07-16
7
234 Views
Last Modified: 2010-03-31
Hi

If I have a file that I know is compressed would the following work... I want to unzip the file and save it as the same file name.  I've tried this a few times.. keeps screwing up the out file....  any ideas...  the zipped file might not have a .zip extension either..

Thanks
ryno71

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

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.zip.CRC32;
import java.util.zip.ZipOutputStream;

public class Unzip
{


public void Unzip(String fileName)
{
      try {
        // Open the ZIP file
        String inFilename = fileName;
            System.out.println ("inFilename is " + inFilename);
        ZipInputStream in = new ZipInputStream(new FileInputStream(inFilename));
       
        // Get the first entry
       
            String outFilename =fileName;
            
            System.out.println ("outFilenameis " + outFilename);
        // Open the output file
       
        OutputStream out = new FileOutputStream(outFilename);
   
        // Transfer bytes from the ZIP file to the output file
        byte[] buf = new byte[1024];
        int len;
        while ((len = in.read(buf)) > 0) {
            out.write(buf, 0, len);
        }
   
        // Close the streams
        out.close();
        in.close();
    } catch (IOException e)
      {
    }
      

}

static public void main(String[] args)
      {
            String output="output.txt";
            Unzip uk = new Unzip();
            uk.Unzip(output);
            
      }
}
0
Comment
Question by:ryno71
  • 4
  • 3
7 Comments
 
LVL 23

Expert Comment

by:Ajay-Singh
ID: 17117736
> OutputStream out = new FileOutputStream(outFilename);

add the following line after this
ZipEntry entry = in.getNextEntry();
0
 

Author Comment

by:ryno71
ID: 17117758
>>ZipEntry entry = in.getNextEntry();

keeps the file from becoming corrupted?


ZipEntry entry = in.getNextEntry();
OutputStream out = new FileOutputStream(outFilename);
0
 
LVL 23

Accepted Solution

by:
Ajay-Singh earned 125 total points
ID: 17117765
> keeps the file from becoming corrupted?

Just moves the stream pointer to the next entry of the zip file from where it can be read.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:ryno71
ID: 17117794
thx
0
 

Author Comment

by:ryno71
ID: 17117835
so it shouldn't matter if the file is of a zip extension or not.....  starting to think it did
0
 
LVL 23

Expert Comment

by:Ajay-Singh
ID: 17117864
yes, extension should not matter. but it should be compressed.
0
 

Author Comment

by:ryno71
ID: 17117875
thanks again!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now