有没有好看的免费的直播-下载windows7
![解压缩软件下载](/uploads/image/0104.jpg)
2023年4月4日发(作者:filewatcher)
Java⼯具类-Zip压缩解压
注意:此类中⽤到的压缩类ZipEntry等都来⾃于包⽽⾮包
依赖:
;
til;
;
Factory;
.*;
ation;
/**
*
zip⼯具类
*
*
注意:此类中⽤到的压缩类ZipEntry等都来⾃于包⽽⾮包
*
依赖:
*
*@authorhezhao
*@Time2017年7⽉28⽇下午3:23:41
*/
publicclassZipUtil{
privatestaticfinalLoggerlogger=ger();
/**
*使⽤GBK编码可以避免压缩中⽂⽂件名乱码
*/
privatestaticfinalStringCHINESE_CHARSET="GBK";
/**
*⽂件读取缓冲区⼤⼩
*/
privatestaticfinalintCACHE_SIZE=1024;
privateZipUtil(){
//私⽤构造主法.因为此类是⼯具类.
}
/**
*
*压缩⽂件
*
*
*@paramsourceFolder需压缩⽂件或者⽂件夹路径
*@paramzipFilePath压缩⽂件输出路径
*@throwsException
*/
publicstaticvoidzip(StringsourceFolder,StringzipFilePath)throwsException{
("开始压缩["+sourceFolder+"]到["+zipFilePath+"]");
OutputStreamout=newFileOutputStream(zipFilePath);
BufferedOutputStreambos=newBufferedOutputStream(out);
putStreamzos=putStream(bos);
//解决中⽂⽂件名乱码
oding(CHINESE_CHARSET);
Filefile=newFile(sourceFolder);
StringbasePath=null;
if(ctory()){
basePath=h();
}else{
basePath=ent();
}
zipFile(file,basePath,zos);
ntry();
();
();
();
();
("压缩["+sourceFolder+"]完成!");
}
/**
*
*压缩⽂件
*
*
*@paramsourceFolders⼀组压缩⽂件夹或⽂件
*@paramzipFilePath压缩⽂件输出路径
*@throwsException
*/
publicstaticvoidzip(String[]sourceFolders,StringzipFilePath)throwsException{
OutputStreamout=newFileOutputStream(zipFilePath);
BufferedOutputStreambos=newBufferedOutputStream(out);
putStreamzos=putStream(bos);
//解决中⽂⽂件名乱码
oding(CHINESE_CHARSET);
for(inti=0;i<;i++){
("开始压缩["+sourceFolders[i]+"]到["+zipFilePath+"]");
Filefile=newFile(sourceFolders[i]);
StringbasePath=null;
basePath=ent();
zipFile(file,basePath,zos);
}
ntry();
();
();
();
("压缩"+(sourceFolders)+"完成!");
}
/**
*
*递归压缩⽂件
*
*
*@paramparentFile
*@parambasePath
*@paramzos
*@throwsException
*/
privatestaticvoidzipFile(FileparentFile,StringbasePath,putStreamzos)throwsException{
File[]files=newFile[0];
if(ctory()){
files=les();
}else{
files=newFile[1];
files[0]=parentFile;
}
StringpathName;
InputStreamis;
BufferedInputStreambis;
byte[]cache=newbyte[CACHE_SIZE];
for(Filefile:files){
if(ctory()){
("⽬录:"+h());
basePath=e('','/');
if(ing(()-1).equals("/")){
pathName=h().substring(())+"/";
}else{
pathName=h().substring(()+1)+"/";
}
tEntry(ry(pathName));
zipFile(file,basePath,zos);
}else{
pathName=h().substring(());
pathName=e('','/');
if(ing(0,1).equals("/")){
pathName=ing(1);
}
("压缩:"+pathName);
is=newFileInputStream(file);
bis=newBufferedInputStream(is);
tEntry(ry(pathName));
intnRead=0;
while((nRead=(cache,0,CACHE_SIZE))!=-1){
(cache,0,nRead);
}
();
();
}
}
}
/**
*解压zip⽂件
*
*@paramzipFileName
*待解压的zip⽂件路径,例如:c:
*
*@paramoutputDirectory
*解压⽬标⽂件夹,例如:c:a
*/
publicstaticvoidunZip(StringzipFileName,StringoutputDirectory)
throwsException{
("开始解压["+zipFileName+"]到["+outputDirectory+"]");
ezipFile=e(zipFileName);
try{
Enumeration<?>e=ries();
ryzipEntry=null;
createDirectory(outputDirectory,"");
while(eElements()){
zipEntry=(ry)ement();
("解压:"+e());
if(ctory()){
Stringname=e();
name=ing(0,()-1);
Filef=newFile(outputDirectory+tor+name);
();
("创建⽬录:"+outputDirectory+tor+name);
}else{
StringfileName=e();
fileName=e('','/');
if(f("/")!=-1){
createDirectory(outputDirectory,ing(0,
dexOf("/")));
fileName=ing(
dexOf("/")+1,
dexOf("/")+1,
());
}
Filef=newFile(outputDirectory+tor
+e());
NewFile();
InputStreamin=utStream(zipEntry);
FileOutputStreamout=newFileOutputStream(f);
byte[]by=newbyte[1024];
intc;
while((c=(by))!=-1){
(by,0,c);
}
();
();
}
}
("解压["+zipFileName+"]完成!");
}catch(Exceptionex){
n(sage());
}finally{
();
}
}
/**
*创建⽬录
*@authorhezhao
*@Time2017年7⽉28⽇下午7:10:05
*@paramdirectory
*@paramsubDirectory
*/
privatestaticvoidcreateDirectory(Stringdirectory,StringsubDirectory){
Stringdir[];
Filefl=newFile(directory);
try{
if(subDirectory==""&&()!=true){
();
}elseif(subDirectory!=""){
dir=e('','/').split("/");
for(inti=0;i<;i++){
FilesubFile=newFile(directory+tor+dir[i]);
if(()==false)
();
directory+=tor+dir[i];
}
}
}catch(Exceptionex){
n(sage());
}
}
/**
*⽆需解压直接读取Zip⽂件和⽂件内容
*@authorhezhao
*@Time2017年7⽉28⽇下午3:23:10
*@paramfile⽂件
*@throwsException
*/
publicstaticvoidreadZipFile(Stringfile)throwsException{
ezipFile=e(file);
InputStreamin=newBufferedInputStream(newFileInputStream(file));
utStreamzin=utStream(in);
ryze;
while((ze=tEntry())!=null){
if(ctory()){
}else{
("file-"+e()+":"
+e()+"bytes");
longsize=e();
if(size>0){
BufferedReaderbr=newBufferedReader(
newInputStreamReader(utStream(ze)));
Stringline;
while((line=ne())!=null){
n(line);
}
();
}
n();
}
}
ntry();
}
publicstaticvoidmain(String[]args)throwsException{
try{
//readZipFile("D:new1⽂字.zip");
//压缩⽂件
//StringsourceFolder="D:/新建⽂本⽂档.txt";
//StringzipFilePath="D:/新建⽂本⽂档.zip";
//(sourceFolder,zipFilePath);
//压缩⽂件夹
//StringsourceFolder="D:/fsc1";
//StringzipFilePath="D:/";
//(sourceFolder,zipFilePath);
//压缩⼀组⽂件
//String[]paths={"D:/新建⽂本⽂档.txt","D:","D:/new1"};
//zip(paths,"D:/");
//unZip("D:","D:/fsc2");
}catch(Exceptione){
tackTrace();
}
}
}
更多推荐
解压缩软件下载
发布评论