Skip to content

Instantly share code, notes, and snippets.

@chiehmin
Last active December 20, 2015 03:19
Show Gist options
  • Save chiehmin/6062786 to your computer and use it in GitHub Desktop.
Save chiehmin/6062786 to your computer and use it in GitHub Desktop.
import java.io.*;
import java.util.zip.*;
public class Zip{
private ZipInputStream zipIn;
private ZipOutputStream zipOut;
private ZipEntry zipEntry;
private static int bufSize;
private byte[] buf;
private int readedBytes;
public Zip(){
this(512);
}
public Zip(int bufSize){
this.bufSize = bufSize;
this.buf = new byte[this.bufSize];
}
public void doZip(String zipDirectory){
File file;
File zipDir;
zipDir = new File(zipDirectory);
String zipFileName = zipDir.getName() + ".zip";
try{
this.zipOut = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(zipFileName)));
handleDir(zipDir , this.zipOut);
this.zipOut.close();
}catch(IOException ioe){
ioe.printStackTrace();
}
}
private void handleDir(File dir , ZipOutputStream zipOut)throws IOException{
FileInputStream fileIn;
File[] files;
files = dir.listFiles();
if(files.length == 0){
this.zipOut.putNextEntry(new ZipEntry(dir.toString() + "/"));
this.zipOut.closeEntry();
}
else{
for(File fileName : files){
if(fileName.isDirectory()){
handleDir(fileName , this.zipOut);
}
else{
fileIn = new FileInputStream(fileName);
this.zipOut.putNextEntry(new ZipEntry(fileName.toString()));
while((this.readedBytes = fileIn.read(this.buf))>0){
this.zipOut.write(this.buf , 0 , this.readedBytes);
}
this.zipOut.closeEntry();
}
}
}
}
public void unZip(String unZipfileName){
FileOutputStream fileOut;
File file;
try{
this.zipIn = new ZipInputStream (new BufferedInputStream(new FileInputStream(unZipfileName)));
while((this.zipEntry = this.zipIn.getNextEntry()) != null){
file = new File(this.zipEntry.getName());
if(this.zipEntry.isDirectory()){
file.mkdirs();
}
else{
File parent = file.getParentFile();
if(!parent.exists()){
parent.mkdirs();
}
fileOut = new FileOutputStream(file);
while(( this.readedBytes = this.zipIn.read(this.buf) ) > 0){
fileOut.write(this.buf , 0 , this.readedBytes );
}
fileOut.close();
}
this.zipIn.closeEntry();
}
}catch(IOException ioe){
ioe.printStackTrace();
}
}
public void setBufSize(int bufSize){
this.bufSize = bufSize;
}
public static void main(String[] args)throws Exception{
if(args.length==2){
String name = args[1];
Zip zip = new Zip();
if(args[0].equals("-zip"))
zip.doZip(name);
else if(args[0].equals("-unzip"))
zip.unZip(name);
}
else{
System.out.println("Usage:");
System.out.println("Compress: java Zip -zip directoryName");
System.out.println("Extract: java Zip -unzip fileName.zip");
throw new Exception("Arguments error!");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment