What's new

Closed Creating zipfile using zipoutputstream

Status
Not open for further replies.

Eyaa-

Eternal Poster
Joined
Jul 22, 2016
Posts
607
Reaction
373
Points
266
This Java example shows how create zip file containing one file using Java ZipOutputStream class.
import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.IOException;

import java.util.zip.ZipEntry;

import java.util.zip.ZipException;

import java.util.zip.ZipOutputStream;



public class CreateZipFile {
public static void main(String args[])
{
{
String zipFile = "C:/FileIO/zipdemo.zip";
String sourceFile = "C:/FileIO/sourcefile.doc";
byte[] buffer = new byte[1024];
FileOutputStream fout = new FileOutputStream(zipFile);
ZipOutputStream zout = new ZipOutputStream(fout);
FileInputStream fin = new FileInputStream(sourceFile);
zout.putNextEntry(new ZipEntry(sourceFile));
int length;
while((length = fin.read(buffer)) > 0)
{
zout.write(buffer, 0, length);
}
zout.closeEntry();
fin.close();
zout.close();
System.out.println("Zip file has been created!");

}

catch(IOException ioe)
{
System.out.println("IOException :" + ioe);
}
}
}
kung may gusto kayo I pwede nyo gamitin tong codes na to use zip files
 
Status
Not open for further replies.

Similar threads

Back
Top