Solved

Unzip question

Posted on 2006-07-16
7
235 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 

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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
nextBoolean(double p) for Random class 3 40
couple of eclipse 5 35
Why my table column Id is not passed to java object? 4 38
hibernate example for saving data 19 37
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
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.

813 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

12 Experts available now in Live!

Get 1:1 Help Now