TA的每日心情 | 开心 2021-3-12 23:18 |
---|
签到天数: 2 天 [LV.1]初来乍到
|
- 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;
- }
- private File[] getFiles() {//获取源目录的所有图像文件
- 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("JPG") || ext.equals("GIF");
- }
- });
- return files;
- }
- public static void main(String[] args) {
- String srcPath = args[0];//源图像的目录
- if (srcPath==null){
- printHelp();
- return;
- }
-
- FileMgr fm = new FileMgr(srcPath);
- File[] fi=fm.getFiles();
- for(int i=0;i< fi.length;i++){
-
- fm.process(srcPath,fi[i]);
- }
- }
- public static void printHelp(){
- System.out.println("USAGE: java FileMgr < FILEPATH>");
- }
- /**
- *压缩
- */
- 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/29/235644953.zip |
|