import java.io.File;
public class ZipCompressorByAnt {
private File zipFile;
/**
* 压缩文件构造函数
* @param pathName 最终压缩生成的压缩文件:目录+压缩文件名.zip
*/
public ZipCompressorByAnt(String finalFile) {
zipFile = new File(finalFile);
}
/**
* 执行压缩操作
* @param srcPathName 需要被压缩的文件/文件夹
*/
public void compressExe(String srcPathName) {
System.out.println("srcPathName="+srcPathName);
File srcdir = new File(srcPathName);
if (!srcdir.exists()){
throw new RuntimeException(srcPathName + "不存在!");
}
Project prj = new Project();
Zip zip = new Zip();
zip.setProject(prj);
zip.setDestFile(zipFile);
FileSet fileSet = new FileSet();
fileSet.setProject(prj);
fileSet.setDir(srcdir);
//fileSet.setIncludes("**/*.java"); //包括哪些文件或文件夹 eg:zip.setIncludes("*.java");
//fileSet.setExcludes(...); //排除哪些文件或文件夹
zip.addFileset(fileSet);
zip.execute();
}
}
public class TestZip {
public static void main(String[] args) {
ZipCompressorByAnt zca = new ZipCompressorByAnt("E:\test1.zip ");
zca.compressExe("E:\test1");
}
}
/*如果 出现ant 的 52 51 50 等版本问题 可以去找对应的ant-1.8.2.jar 我开始用的ant-1.10.1.jar 就是这个包版本高了 一直报verson 52 版本问题*/
② 电脑文件zip如何压缩java实现对zip文件的压缩
电脑文件zip如何压缩(java实现对zip文件的压缩)
一、java实现压缩为zip
importjava.io.File;
importjava.io.FileInputStream;
importjava.io.IOException;
importjava.io.OutputStream;
importjava.util.zip.ZipEntry;
importjava.util.zip.ZipOutputStream;
publicclassZipUtils{
privatestaticfinalintBUFFER_SIZE=2*1024;
publicstaticvoiddirFile(Filedir){
File[]files=dir.listFiles();//得到File数组,获得目录下所有文件
for(Filefile:files){//遍历所有的子目录和文件
if(file.isDirectory()){
dirFile(file);//如果是目录递归调用dirFile()
}
//成功压缩文件后,对原文件进行删除
file.delete();
}
//顺带把对应的目录进行删除
dir.delete();
}
/**
*压缩成ZIP方法1
*@paramsrcDir压缩文件夹路径
*@paramout压缩文件输出流
*@paramKeepDirstructure是否保留原来的目录结构,true:保留目录结构;
*false:所有文件跑到压缩包根目录下(注意:不保留目录结构可能会出现同名文件,会压缩失败)
*@throwsRuntimeException压缩失败会抛出运行时异常
*/
publicstaticvoidtoZip(StringsrcDir,OutputStreamout,booleanKeepDirstructure)
throwsRuntimeException{
longstart=System.currentTimeMillis();
ZipOutputStreamzos=null;
try{
zos=newZipOutputStream(out);
FilesourceFile=newFile(srcDir);
compress(sourceFile,zos,sourceFile.getName(),KeepDirstructure);
longend=System.currentTimeMillis();
System.out.println("压缩完成,耗时:"+(end-start)+"ms");
}catch(Exceptione){
thrownewRuntimeException("ziperrorfromZipUtils",e);
}finally{
if(zos!=null){
try{
zos.close();
}catch(IOExceptione){
e.printstacktrace();
}
}
}
}
/**
*递归压缩方法
*@paramsourceFile源文件
*@paramzoszip输出流
*@paramname压缩后的名称
*@paramKeepDirstructure是否保留原来的目录结构,会压缩失败)
*@throwsException
*/
privatestaticvoidcompress(FilesourceFile,ZipOutputStreamzos,Stringname,
booleanKeepDirstructure)throwsException{
byte[]buf=newbyte[BUFFER_SIZE];
if(sourceFile.isFile()){
//向zip输出流中添加一个zip实体,构造器中name为zip实体的文件的名字
zos.putNextEntry(newZipEntry(name));
//文件到zip输出流中
intlen;
FileInputStreamin=newFileInputStream(sourceFile);
while((len=in.read(buf))!=-1){
zos.write(buf,len);
}
//Completetheentry
zos.closeEntry();
in.close();
}else{
File[]listFiles=sourceFile.listFiles();
if(listFiles==null||listFiles.length==0){
//需要保留原来的文件结构时,需要对空文件夹进行处理
if(KeepDirstructure){
//空文件夹的处理
zos.putNextEntry(newZipEntry(name+"/"));
//没有文件,不需要文件的
zos.closeEntry();
}
}else{
for(Filefile:listFiles){
//判断是否需要保留原来的文件结构
if(KeepDirstructure){
//注意:file.getName()前面需要带上父文件夹的名字加一斜杠,
//不然最后压缩包中就不能保留原来的文件结构,即:所有文件都跑到压缩包根目录下了
compress(file,name+"/"+file.getName(),KeepDirstructure);
}else{
compress(file,file.getName(),KeepDirstructure);
}
}
}
}
}
}
③ java文件写入后下载下来为啥是压缩包
该原因是在写入文件时使用了压缩算法,或者在下载文件时使用了压缩格式。
如果在写入文件时使用了压缩算法,例如使用GZIPOutputStream或ZipOutputStream等类来写入文件,那么写入的文件就是压缩文件。
在下载文件时,如果使用了压缩格式,例如ZIP或GZIP等格式,那么下载下来的文件就是压缩包。