Import Java.io.BufferedInputStream;
Import Java.io.BufferedOutputStream;
Import Java.io.File;
Import Java.io.FileInputStream;
Import java.io.FileNotFoundException;
Import Java.io.FileOutputStream;
Import java.io.IOException;
Import Java.util.zip.ZipEntry;
Import Java.util.zip.ZipOutputStream;
/**
* Package files into zip compressed files
* @author JiangYang
* @since 2013-6-28
*/
Public final class Packfile {
Private Packfile () {
}
/**
* will be stored in the Sourcefilepath directory of the source files, packaged into the filename name of the zip file, and stored to Zipfilepath.
* @param sourcefilepath file path to be compressed
* @param zipfilepath after compression storage path
* @param filename Compressed file name
* @return Flag
*/
public static Boolean Filetozip (String sourcefilepath,string zipfilepath,string fileName) {
Boolean flag = false;
File SourceFile = new file (Sourcefilepath);
FileInputStream FIS = null;
Bufferedinputstream bis = null;
FileOutputStream fos = null;
Zipoutputstream Zos = null;
if (sourcefile.exists () = = False) {
System.out.println (">>>>>> to be compressed file directory: + Sourcefilepath +" does not exist. <<<<<< ");
} else {
try {
File ZipFile = new file (Zipfilepath + "/" + FileName + ". zip");
if (zipfile.exists ()) {
System.out.println (">>>>>>" + Zipfilepath +) directory exists under the name: "+ fileName +". zip "+" packaging files. <<<<<< ");
} else {
file[] SourceFiles = Sourcefile.listfiles ();
if (null = = SourceFiles | | Sourcefiles.length < 1) {
System.out.println (">>>>>> to be compressed file directory: + Sourcefilepath +" does not exist in the file, without compression. <<<<<< ");
} else {
FOS = new FileOutputStream (ZipFile);
Zos = new Zipoutputstream (new Bufferedoutputstream (FOS));
byte[] Bufs = new BYTE[1024*10];
for (int i=0;i<sourcefiles.length;i++) {
Create a zip entity and add a compressed package
ZipEntry zipentry = new ZipEntry (Sourcefiles[i].getname ());
Zos.putnextentry (ZipEntry);
Read the file to be compressed and write it into a compressed package
FIS = new FileInputStream (sourcefiles[i]);
bis = new Bufferedinputstream (FIS,1024*10);
int read = 0;
while ((Read=bis.read (bufs, 0, 1024*10))!=-1) {
Zos.write (bufs, 0, read);
}
}
Flag = true;
}
}
catch (FileNotFoundException e) {
E.printstacktrace ();
throw new RuntimeException (e);
catch (IOException e) {
E.printstacktrace ();
throw new RuntimeException (e);
finally {
Close the stream
try {
if (null!= bis) bis.close ();
if (null!= zos) zos.close ();
catch (IOException e) {
E.printstacktrace ();
throw new RuntimeException (e);
}
}
}
return flag;
}
/**
* Package files into Zip compressed file, Main method test
* @param args
*/
public static void Main (string[] args) {
String Sourcefilepath = "d:\\11";
String Zipfilepath = "d:\\";
String fileName = "Test";
Boolean flag = Packfile.filetozip (Sourcefilepath, Zipfilepath, fileName);
if (flag) {
System.out.println (">>>>>> file package successful. <<<<<< ");
} else {
System.out.println (">>>>>> file pack failed. <<<<<< ");
}
}
}