zoukankan      html  css  js  c++  java
  • zip解压缩

    package com.green.project.compress;

    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.util.zip.ZipEntry;
    import java.util.zip.ZipInputStream;

    public class Unzip {

    /**
    * @param args
    */
    byte doc[]=null;
    String Filename=null;
    String unzipPath=null;

    public Unzip(String filename,String unzippath)
    {
    Filename=filename;
    unzipPath=unzippath;
    SetUnZipPath(unzipPath);
    }

    public Unzip(String filename)
    {
    Filename=filename;
    unzipPath=null;
    SetUnZipPath(unzipPath);
    }

    private void SetUnZipPath(String unzippath)
    {
    if(unzippath.endsWith("//")) //String类的endsWith(char c)用与判断字符串最后一个字符是否与c相同
    unzipPath=new String(unzippath);
    else
    unzipPath=new String(unzippath+"//");
    }

    public void doUnZip()
    {
    try {
    ZipInputStream zin=new ZipInputStream(new FileInputStream(Filename));
    ZipEntry fentry; //用于表示 ZIP 文件条目
    while((fentry=zin.getNextEntry())!=null) //ZipInputStream类的geNextEntry()读取下一个 ZIP 文件条目并将流定位到该条目数据的开始处。
    {
    if(fentry.isDirectory()) //判断文件条目是否目录条目
    checkFilePath(unzipPath+fentry.getName());
    else
    {
    String fname=new String(unzipPath+fentry.getName());
    FileOutputStream out=new FileOutputStream(fname);
    doc=new byte[512];
    int n;
    while((n=zin.read(doc, 0, 512))!=-1)out.write(doc,0,n);
    out.close();
    out=null;
    doc=null;
    }
    }//while
    zin.close();
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }

    }

    private void checkFilePath(String dirName)throws IOException
    {
    File dir=new File(dirName);
    if(!dir.exists())
    dir.mkdirs();
    }

    public static void main(String[] args) {
    Unzip myZip=new Unzip("d://phonemanage.zip","d://lib");
    myZip.doUnZip();
    System.out.println("压缩成功");
    }

    }

  • 相关阅读:
    生产上第一使用线程池后的总结与反思
    20190407
    20190403
    Asp.net MVC中的ViewData与ViewBag
    easyui datagrid分页
    EF从数据库更新模型更新不到新表
    C语言 笔记(函数)
    python 写100~1000以内水仙花数
    python 求前n项阶乘的和
    python 写九九乘法表
  • 原文地址:https://www.cnblogs.com/maybo/p/5182472.html
Copyright © 2011-2022 走看看