TA的每日心情 | 开心 2021-3-12 23:18 |
---|
签到天数: 2 天 [LV.1]初来乍到
|
简单的压缩程序,将某一目录下所有的bmp图像文件分别压缩。 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 <FILEPATH>");
}
- 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());
- }
- }
- }
复制代码
源码下载:http://file.javaxxz.com/2014/10/2/082143141.zip |
|