当前位置:首页 » 文件管理 » java解压缩工具类

java解压缩工具类

发布时间: 2025-03-02 14:06:53

‘壹’ java如何解压页面上传到服务器的zip文件

这个转换肯定是会出错的,struts 的formFile跟zipFile没有直接关系,怎么能这么强制转化呢?
建议
1. 把文件保存到一个临时目录(保存为zip文件)
2. 读取这个文件
3. 抽取想要的文件
4. 把临时文件删除

‘贰’ 如何使用java压缩文件夹成为zip包

在JDK中有一个zip工具类:

java.util.zip Provides classes for reading and writing the standard ZIP and
GZIP file formats.

使用此类可以将文件夹或者多个文件进行打包压缩操作。

在使用之前先了解关键方法:

ZipEntry(String name) Creates a new zip entry with the specified name.

使用ZipEntry的构造方法可以创建一个zip压缩文件包的实例,然后通过ZipOutputStream将待压缩的文件以流的形式写进该压缩包中。具体实现代码如下:

importjava.io.BufferedInputStream;
importjava.io.BufferedOutputStream;
importjava.io.File;
importjava.io.FileInputStream;
importjava.io.FileNotFoundException;
importjava.io.FileOutputStream;
importjava.io.IOException;
importjava.util.zip.ZipEntry;
importjava.util.zip.ZipOutputStream;
/**
*将文件夹下面的文件
*打包成zip压缩文件
*
*@authoradmin
*
*/
publicfinalclassFileToZip{

privateFileToZip(){}

/**
*将存放在sourceFilePath目录下的源文件,打包成fileName名称的zip文件,并存放到zipFilePath路径下
*@paramsourceFilePath:待压缩的文件路径
*@paramzipFilePath:压缩后存放路径
*@paramfileName:压缩后文件的名称
*@return
*/
publicstaticbooleanfileToZip(StringsourceFilePath,StringzipFilePath,StringfileName){
booleanflag=false;
FilesourceFile=newFile(sourceFilePath);
FileInputStreamfis=null;
BufferedInputStreambis=null;
FileOutputStreamfos=null;
ZipOutputStreamzos=null;

if(sourceFile.exists()==false){
System.out.println("待压缩的文件目录:"+sourceFilePath+"不存在.");
}else{
try{
FilezipFile=newFile(zipFilePath+"/"+fileName+".zip");
if(zipFile.exists()){
System.out.println(zipFilePath+"目录下存在名字为:"+fileName+".zip"+"打包文件.");
}else{
File[]sourceFiles=sourceFile.listFiles();
if(null==sourceFiles||sourceFiles.length<1){
System.out.println("待压缩的文件目录:"+sourceFilePath+"里面不存在文件,无需压缩.");
}else{
fos=newFileOutputStream(zipFile);
zos=newZipOutputStream(newBufferedOutputStream(fos));
byte[]bufs=newbyte[1024*10];
for(inti=0;i<sourceFiles.length;i++){
//创建ZIP实体,并添加进压缩包
ZipEntryzipEntry=newZipEntry(sourceFiles[i].getName());
zos.putNextEntry(zipEntry);
//读取待压缩的文件并写进压缩包里
fis=newFileInputStream(sourceFiles[i]);
bis=newBufferedInputStream(fis,1024*10);
intread=0;
while((read=bis.read(bufs,0,1024*10))!=-1){
zos.write(bufs,0,read);
}
}
flag=true;
}
}
}catch(FileNotFoundExceptione){
e.printStackTrace();
thrownewRuntimeException(e);
}catch(IOExceptione){
e.printStackTrace();
thrownewRuntimeException(e);
}finally{
//关闭流
try{
if(null!=bis)bis.close();
if(null!=zos)zos.close();
}catch(IOExceptione){
e.printStackTrace();
thrownewRuntimeException(e);
}
}
}
returnflag;
}

publicstaticvoidmain(String[]args){
StringsourceFilePath="D:\TestFile";
StringzipFilePath="D:\tmp";
StringfileName="12700153file";
booleanflag=FileToZip.fileToZip(sourceFilePath,zipFilePath,fileName);
if(flag){
System.out.println("文件打包成功!");
}else{
System.out.println("文件打包失败!");
}
}

}
热点内容
unix与linux 发布:2025-05-02 14:26:21 浏览:955
lol内部脚本 发布:2025-05-02 14:21:34 浏览:706
怎么代理服务器ip 发布:2025-05-02 14:18:08 浏览:206
手机屏幕锁住了忘了密码怎么办 发布:2025-05-02 14:16:49 浏览:244
c语言拼写检查 发布:2025-05-02 14:04:49 浏览:1002
编译c的ide 发布:2025-05-02 14:04:40 浏览:160
360云盘下载文件夹 发布:2025-05-02 14:04:37 浏览:491
tft新加坡服务器怎么下载 发布:2025-05-02 13:54:04 浏览:252
安卓系统为什么称中国系统 发布:2025-05-02 13:36:32 浏览:943
有什么软件可以让安卓手机变苹果 发布:2025-05-02 12:54:52 浏览:618