首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2SE开发 >

将单个文件压缩成zip文件解决思路

2012-01-20 
将单个文件压缩成zip文件就是将程序生成的文件再压缩成zip包,命名规则是原文件名的一部分,如:123-123-123.

将单个文件压缩成zip文件
就是将程序生成的文件再压缩成zip包,命名规则是原文件名的一部分,如:123-123-123.xml to 123-123.zip 请问各位该如何处理

[解决办法]

Java code
private void zip(File file) throws IOException {        if(!file.exists())            throw new FileNotFoundException();        if(file.isDirectory())            throw new FileNotFoundException();                String newName = file.getName();        if(newName.lastIndexOf('.')!=-1){            newName = newName.substring(0, newName.lastIndexOf('.'));        }                newName += ".zip";                File newFile = new File(file.getParent(),newName);                ZipOutputStream out = new ZipOutputStream(new FileOutputStream(newFile));                ZipEntry ze = new ZipEntry(file.getName());                out.putNextEntry(ze);                FileInputStream in = new FileInputStream(file);                byte[] bfs = new byte[1024];        int rr;        while((rr = in.read(bfs))!=-1){            out.write(bfs, 0, rr);        }        in.close();                        out.flush();                out.closeEntry();        out.close();    } 

热点排行
Bad Request.