62,623
社区成员
发帖
与我相关
我的任务
分享
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();
}