import java.io.*;
import java.util.*;
import java.util.zip.ZipOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
public class FileMgr {
String srcPath;
public FileMgr(String srcPath) {
this.srcPath=srcPath;
}
public static void printHelp(){
System.out.println("USAGE: java FileMgr
}
public static void main(String[] args) { if (args.length!=1){ printHelp(); return; } String srcPath = args[0];//源图像的目录 FileMgr fm = new FileMgr(srcPath); File[] fi=fm.getFiles(); for(int i=0;i< fi.length;i++){ fm.process(srcPath,fi[i]); } } private File[] getFiles() {//获取源目录中的所有bmp图像文件 File path = new File(srcPath); File[] files = path.listFiles(new FileFilter() { public boolean accept(File pathname) { if (pathname == null) return false; String ext = pathname.getName().substring(pathname.getName().lastIndexOf(".") + 1).toUpperCase(); return ext.equals("BMP"); } }); return files; } /** *压缩 */ public void process(String srcPath,File srcfile) { //srcfile 需要压缩的文件 int k=srcfile.getName().indexOf("."); String fn=srcfile.getName().substring(0,k); System.out.println("OK--"+fn+".zip"); byte[] buf = new byte[1024]; try { // 创建一个压缩文件 ZipOutputStream out = new ZipOutputStream(new FileOutputStream(srcPath+"/"+fn+".zip")); // 压缩创建的文件 FileInputStream in = new FileInputStream(srcPath+"/"+srcfile.getName()); ZipEntry entry=new ZipEntry(srcfile.getName()); out.putNextEntry(entry); int len; while ( (len = in.read(buf)) > 0) { out.write(buf, 0, len); } in.close(); // 完成此压缩文件 out.close(); }catch (IOException e) { System.out.println(e.toString()); } } }</td> </tr> <tr>
↑返回目录
前一篇: 深入浅出面向对象的复用
后一篇: java编程技巧两则