Solved

Unzip question

Posted on 2006-07-16
7
238 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 

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

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.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
eclipse console opening separately 2 50
Is there a simpler dropbox system? 10 53
Selenium docs api java index 3 98
How does initial implicit objects  are set up in JSP? 7 41
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
This video teaches viewers about errors in exception handling.
Suggested Courses

710 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