请问如何在Java中把一个文件压缩成一个*.zip文件??

wulingdongliu 2003-01-27 07:40:49
请问如何在Java中把一个文件压缩成一个*.zip文件??
用一个类来实现,要起到压缩的功能
很急用!
知道了请马上告诉我呀
谢谢
...全文
33 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
huanddehualt 2003-04-29
  • 打赏
  • 举报
回复
中文文件或目录有问题...
有高人说是sun的库的问题...
详见:
http://expert.csdn.net/Expert/topic/1183/1183995.xml?temp=.4020197
huanddehualt 2003-04-29
  • 打赏
  • 举报
回复
import java.util.zip.*;
import java.io.*;
import java.util.*;

public class CompressT {
public CompressT() {}
public static int dozip(String strfp,String strzip){
try{
ZipOutputStream zipout = new ZipOutputStream( new CheckedOutputStream(new FileOutputStream(strzip),new CRC32()));
creatZipEntry(strfp,zipout,"");
zipout.close();
}catch(Exception e){System.out.println(e.toString()); return -1;}
return 1;
}
private static void creatZipEntry(String strfilepath,ZipOutputStream out,String relativeDir)
throws FileNotFoundException,IOException {
File file = new File(strfilepath);
String[] strlist =file.list();
if(strlist==null||strlist.length<=0) return;
for(int i=0;i<strlist.length;i++){
String AllfileName = file.getPath().replace('\\','/')+"/"+strlist[i];
File dir = new File(AllfileName);
if(dir.isDirectory()) {
ZipEntry ze2 = new ZipEntry(relativeDir+strlist[i]+"/");
out.putNextEntry(ze2);
creatZipEntry(AllfileName,out,relativeDir+strlist[i]+"/");
continue;
}//if(dir.isDirectory()) {
ZipEntry ze1 = new ZipEntry(relativeDir+strlist[i]);
out.putNextEntry(ze1);
FileInputStream f = new FileInputStream(AllfileName) ;
byte[] b=new byte[f.available()];
f.read(b);
out.write(b);
f.close();
out.closeEntry();
}//for(int i=0;i<strlist.length;i++){
}
public static void main(String[] args) {
CompressT.dozip("d:/test","d:/test.zip") ;
}
}
DavidBone 2003-04-20
  • 打赏
  • 举报
回复
up
teddy_huang 2003-01-27
  • 打赏
  • 举报
回复
http://expert.csdn.net/Expert/topic/1384/1384424.xml?temp=.3386957
不知道有没有用,我没仔细看
小凳儿 2003-01-27
  • 打赏
  • 举报
回复
使用jdk自带的java.util.zip包.

51,411

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧