zip格式压缩和解压缩(支持中文和文件嵌套解压缩)

野性酷女 2022-09-18 05:43 295阅读 0赞

我在公司项目过程需要用到zip文件的压缩与解压的功能,于是自己研究了一下,找了一些,自己写了一个压缩和解压缩的代码,支持中文,可以文件嵌套(注意其中所用的类是ant.jar中的包中的类,我用的是1.6.0版本)

ExpandedBlockStart.gif ContractedBlock.gif /** */ /**
InBlock.gif *用于对指定源文件路径下的所有文件压缩到指定的文件中
InBlock.gif * @param des String 需要压缩成的文件名
InBlock.gif * @param src String 压缩文件源路径,可以是文件或文件夹
InBlock.gif * @return boolean 如果压缩成功返回true,否则表示失败
ExpandedBlockEnd.gif*/
ExpandedBlockStart.gifContractedBlock.gif public static boolean File2Zip(String des, String src) … {
InBlock.gif
InBlock.gifboolean success =true; //压缩成功标志
InBlock.gif File srcpath =new File(src);
InBlock.gif ZipOutputStream out =null;
ExpandedSubBlockStart.gifContractedSubBlock.giftry…{
InBlock.gif out =new ZipOutputStream(new FileOutputStream(new File(
InBlock.gif des))); //创建压缩输出流
InBlock.gif out.setEncoding(“gbk”);
InBlock.gif compress(srcpath, out, “”); //开始压缩
ExpandedSubBlockEnd.gif }
ExpandedSubBlockStart.gifContractedSubBlock.gifcatch (FileNotFoundException ex) …{
InBlock.gif success =false;
ExpandedSubBlockEnd.gif }
ExpandedSubBlockStart.gifContractedSubBlock.gifcatch (IOException ex1) …{
InBlock.gif success =false;
ExpandedSubBlockEnd.gif }
ExpandedSubBlockStart.gifContractedSubBlock.giffinally…{
ExpandedSubBlockStart.gifContractedSubBlock.gifif (out !=null) …{
ExpandedSubBlockStart.gifContractedSubBlock.giftry…{
InBlock.gif out.close();
ExpandedSubBlockEnd.gif }
ExpandedSubBlockStart.gifContractedSubBlock.gifcatch (IOException ex2) …{
ExpandedSubBlockEnd.gif }
ExpandedSubBlockEnd.gif }
ExpandedSubBlockEnd.gif }
InBlock.gifreturn success;
ExpandedBlockEnd.gif }
None.gif
ExpandedBlockStart.gifContractedBlock.gif /** */ /**
InBlock.gif *
InBlock.gif * @param src File
InBlock.gif * @param out ZipOutputStream
InBlock.gif * @param base String
InBlock.gif * @throws IOException
ExpandedBlockEnd.gif*/
None.gif public static void compress(File src, ZipOutputStream out, String base) throws
ExpandedBlockStart.gifContractedBlock.gif IOException … {
ExpandedSubBlockStart.gifContractedSubBlock.gifif (src.isDirectory()) …{ //如果是目录
InBlock.gif File[] files = src.listFiles(); //列举出目录中所有的内容
InBlock.gif out.putNextEntry(new ZipEntry(base +“/“)); //放入一级目录
InBlock.gif base = base.length() ==0?”” : base +“/“;
ExpandedSubBlockStart.gifContractedSubBlock.giffor (int i =0; i < files.length; i++) …{
InBlock.gif compress(files[i], out, base + files[i].getName());
ExpandedSubBlockEnd.gif }
InBlock.gif
ExpandedSubBlockEnd.gif }
ExpandedSubBlockStart.gifContractedSubBlock.gifelse…{ //如果是文件
ExpandedSubBlockStart.gifContractedSubBlock.gifif(“”.equals(base))…{
InBlock.gif out.putNextEntry(new ZipEntry(base+“/“));
InBlock.gif base=src.getName();
ExpandedSubBlockEnd.gif }
InBlock.gif out.putNextEntry(new ZipEntry(base));
InBlock.gif FileInputStream in =new FileInputStream(src);
InBlock.gifbyte []data=newbyte[4096];
InBlock.gifint b;
ExpandedSubBlockStart.gifContractedSubBlock.gifwhile((b=in.read(data))!=-1)…{
InBlock.gif out.write(data,0,b);
ExpandedSubBlockEnd.gif }
InBlock.gif in.close();
InBlock.gif
ExpandedSubBlockEnd.gif }
ExpandedBlockEnd.gif }
ExpandedBlockStart.gifContractedBlock.gif /** */ /**
InBlock.gif * 解压缩文件
InBlock.gif * @param src String zip所在路径c:/test/kk.zip
InBlock.gif * @param des String 希望存放的目录
InBlock.gif * @throws IOException
InBlock.gif * @throws IOException
ExpandedBlockEnd.gif*/
None.gif public static void decompress(String src, String des) throws IOException
ExpandedBlockStart.gifContractedBlock.gif … {
InBlock.gif ZipFile file=null;
InBlock.gif FileOutputStream fout=null;
InBlock.gif DataOutputStream dout=null;
ExpandedSubBlockStart.gifContractedSubBlock.giftry…{
InBlock.gif file=new ZipFile(src);
InBlock.gif InputStream in=null;
InBlock.gif des=des.replace(‘/‘, ‘/‘);
ExpandedSubBlockStart.gifContractedSubBlock.gifif(des.startsWith(“//“))…{
InBlock.gif des=des.replaceFirst(“//“, “//“);
ExpandedSubBlockEnd.gif }
InBlock.gif Enumeration en=file.getEntries();
ExpandedSubBlockStart.gifContractedSubBlock.gifwhile(en.hasMoreElements())…{
InBlock.gif ZipEntry entry=(ZipEntry)en.nextElement();
ExpandedSubBlockStart.gifContractedSubBlock.gifif(entry.isDirectory())…{ //文件夹
InBlock.gif File directory=new File(des+“/“+entry.getName());
InBlock.gifif(!directory.exists())
InBlock.gif directory.mkdirs();
ExpandedSubBlockStart.gifContractedSubBlock.gif }else…{ //文件
InBlock.gif String path=entry.getName();
InBlock.gif path=path.replace(‘/‘, ‘/‘);
ExpandedSubBlockStart.gifContractedSubBlock.gifif(path.startsWith(“//“))…{
InBlock.gif path=path.replaceFirst(“//“, “//“);
ExpandedSubBlockEnd.gif }
InBlock.gifint pos=path.lastIndexOf(‘/‘);
ExpandedSubBlockStart.gifContractedSubBlock.gifif(pos!=-1)…{
InBlock.gif path=path.substring(0, pos+1);
InBlock.gif File d=new File(des+“/“+path);
InBlock.gifif(!d.exists())
InBlock.gif d.mkdirs();
ExpandedSubBlockEnd.gif }
ExpandedSubBlockStart.gifContractedSubBlock.giftry…{
InBlock.gif File files=new File(entry.getName());
InBlock.gif File f=new File(des+“/“+files.getPath());
InBlock.gif
InBlock.gif fout=new FileOutputStream(f);
InBlock.gif dout=new DataOutputStream(fout);
InBlock.gif in=file.getInputStream(entry);
InBlock.gif
InBlock.gifbyte[] b=newbyte[4096];
InBlock.gifint len =0;
ExpandedSubBlockStart.gifContractedSubBlock.gifwhile ( (len = in.read(b)) !=-1) …{
InBlock.gif dout.write(b, 0, len);
ExpandedSubBlockEnd.gif }
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif }catch(IOException e)…{
InBlock.gifthrow e;
ExpandedSubBlockStart.gifContractedSubBlock.gif }finally…{
InBlock.gifif(fout!=null)
InBlock.gif fout.close();
InBlock.gifif(dout!=null)
InBlock.gif dout.close();
InBlock.gifif(in!=null)
InBlock.gif in.close();
ExpandedSubBlockEnd.gif }
ExpandedSubBlockEnd.gif }
ExpandedSubBlockEnd.gif }
ExpandedSubBlockStart.gifContractedSubBlock.gif }catch(IOException e)…{
InBlock.gifthrow e;
ExpandedSubBlockStart.gifContractedSubBlock.gif }finally…{
InBlock.gifif(file!=null)
InBlock.gif file.close();
InBlock.gifif(fout!=null)
InBlock.gif fout.close();
InBlock.gifif(dout!=null)
InBlock.gif dout.close();
ExpandedSubBlockEnd.gif }
ExpandedBlockEnd.gif }
ExpandedBlockStart.gifContractedBlock.gif /** */ /**
InBlock.gif * 解压缩zip文件
InBlock.gif * @param src String
InBlock.gif * @param des String
InBlock.gif * @return boolean 成功返回true;
ExpandedBlockEnd.gif*/
ExpandedBlockStart.gifContractedBlock.gif public static boolean zipDecompress(String src, String des) … {
InBlock.gifboolean success =true;
ExpandedSubBlockStart.gifContractedSubBlock.giftry…{
InBlock.gif decompress(src, des);
ExpandedSubBlockEnd.gif }
ExpandedSubBlockStart.gifContractedSubBlock.gifcatch (FileNotFoundException ex) …{
InBlock.gif ex.printStackTrace();
InBlock.gif success =false;
ExpandedSubBlockEnd.gif }
ExpandedSubBlockStart.gifContractedSubBlock.gifcatch (IOException ex) …{
InBlock.gif ex.printStackTrace();
InBlock.gif success =false;
ExpandedSubBlockEnd.gif }
InBlock.gif
InBlock.gifreturn success;
ExpandedBlockEnd.gif }

该类有很多不完善的地方,各位可以提意见,共同交流,谢谢

发表评论

表情:
评论列表 (有 0 条评论,295人围观)

还没有评论,来说两句吧...

相关阅读