Solved

Unzip question

Posted on 2006-07-16
7
239 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

 

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

[Webinar] Learn How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Suggested Courses

635 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