Solved

Unzip question

Posted on 2006-07-16
7
233 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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

760 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

21 Experts available now in Live!

Get 1:1 Help Now