import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import java.util.zip.ZipEntry;import java.util.zip.ZipOutputStream;/**
** @author dell-pc*/public class Main {/**
* Creates a zip file*/public void createZipFile() {try {String inputFileName = "test.txt";String zipFileName = "compressed.zip";//Create input and output streamsFileInputStream inStream = new FileInputStream(inputFileName);ZipOutputStream outStream = new ZipOutputStream(new FileOutputStream(zipFileName));// Add a zip entry to the output streamoutStream.putNextEntry(new ZipEntry(inputFileName));byte[] buffer = new byte[1024];int bytesRead;//Each chunk of data read from the input stream//is written to the output streamwhile ((bytesRead = inStream.read(buffer)) > 0) {outStream.write(buffer, 0, bytesRead);}//Close zip entry and file streamsoutStream.closeEntry();outStream.close();inStream.close();} catch (IOException ex) {ex.printStackTrace();}}/**
* @param args the command line arguments*/public static void main(String[] args) {new Main().createZipFile();}}
Comments
Post a Comment