• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Rob Spoor
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Tim Holloway
  • Piet Souris
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Frits Walraven
  • Himai Minh

Could you unzip a file with a java program

 
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have the following zip file and I want to create a program that unzips it. This is my zip file: images.in.gz
 
bob morkos
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Using java.util.zip, a program should use this class by unzipping images.in.gz to images.in. How can this be done and need a sample program that does this. Thanks.
 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi ther....
hope this wil work....for unzip
import java.io.*;
import java.util.zip.*;
public class JUNZIP {
public static void main(String[] args) {
try {


if (args.length != 1) {

System.out.println("Usage: java JUNZIP <inputfile>");
System.exit(1);


}
String filename = null;
if (args[0].endsWith(".gz")) {
filename = args[0].substring(0, args[0].lastIndexOf(".gz"));
} else {
System.out.println("Inputfile must have extension .gz");
System.exit(1);
}
FileOutputStream out = new FileOutputStream(filename);
System.out.println("Extracting archive " + args[0]);
System.out.println("");
FileInputStream file = new FileInputStream(args[0]);
GZIPInputStream in = new GZIPInputStream(file);
FileOutputStream fos = new FileOutputStream(filename);

byte[] data = new byte[2048];
int len;
while ((len = in.read(data)) != -1) {
fos.write(data,0,len);

}
in.close();
out.flush();
out.close();
} catch (Exception e) {

System.out.println( "Exception is " + e.getMessage() );
e.printStackTrace();

}
}
}

for zippppppppp..........

import java.util.zip.*;
import java.io.*;
public class JZip
{
public static void compress_file(String filename, String outfilename)
{
try
{
byte[] buf = new byte[4096]; //read buffer
int retval; //return value
//set up input stream
FileInputStream is = new FileInputStream(filename);
//set up output stream
FileOutputStream os = new FileOutputStream(outfilename);
//wrap output stream in ZipOutputStream
ZipOutputStream zos = new ZipOutputStream(os);
//set entry field in zipfile; do this once for each
//file you're compressing
zos.putNextEntry(new ZipEntry(filename));
//read the input file in and write it to the zipfile
do
{
//read file in from input stream
retval = is.read(buf, 0, 4096);

//-1 indicates end of file
if (retval != -1)
{
//write buffer to output stream
zos.write(buf, 0, retval);
}

}while (retval != -1);
//close this ZipEntry
zos.closeEntry();
//close input stream
is.close();
//close output stream
zos.close();
}
catch(Exception e)
{
System.out.println("Exception occurred: " + e);
}
}
public static void main(String[] argv)
{
if (argv.length != 2)
{
System.out.println("usage: java jZip <filename> <zipfile name>");
return;
}
compress_file (argv[0], argv[1]);
}
}
 
I like tacos! And this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic