⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 filezip.java

📁 培训考试系统代码
💻 JAVA
字号:
package com.huawei.icd30.agt.util.zip;


/**

*/
import java.io.*;
import java.util.zip.*;
import java.util.*;
import java.util.jar.*;
/**
 * 压缩文件工具类,此类完成一般的新建、增加、导出、删除操作以及完成对
 * 压缩文件的内容的解析
 * @author ICD3.0业务 B组-外包-胡云志
 * @version 1.0
 */
public class FileZip
{
  private static FileZip f_instance=null;
  private  boolean isChanged=false;
  private  File selfFile;
  private  File workDirectory=new File(".");
  private  boolean haveSetWorkDirectory=false;
  /**
   * 构造方法,如果指定的文件不存在则创建一个指定名称的新文件
   * @param file 文件
   */
    private FileZip(File file)
    {
        selfFile=file;
        if (!selfFile.exists())
        {
            try
            {
                selfFile.createNewFile();
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
        }
    }

    /**
     * 静态工厂方法,提供本类的唯一实例
     * @param file 要压缩的文件或者文件夹
     * @return FileZip的实例
     */
    public static FileZip getInstance(File file)
    {
        return f_instance=new FileZip(file);
    }

    /**
    * 将目录下的所有文件压缩
    * @param path  目录名
    * @param fileNames  文件数组
    * @throws Exception 异常
    */
    public void addFiles(File path,String[] fileNames) throws Exception
    {
        BufferedInputStream bin= null;
        ZipOutputStream zout = null;
        ZipInputStream zin = null;
        File tmpzip =null;
        ZipEntry[] addedEntries = new ZipEntry[fileNames.length];
        for (int i=0; i<fileNames.length;i++ )
        {
            addedEntries[i]=new ZipEntry(fileNames[i]);
        }
        try
        {
            tmpzip = File.createTempFile("jzj",".tmp",workDirectory);
            zin = new ZipInputStream(new FileInputStream(selfFile));
            OutputStream  aa=new FileOutputStream(tmpzip);
            zout = new ZipOutputStream(aa);
            ZipEntry entry;
            int len = 0;
            byte[] b = new byte[4096];
            for (int i=0;i<addedEntries.length;i++)
            {
                zout.putNextEntry(new ZipEntry(addedEntries[i].getName()));
                bin =new BufferedInputStream(new FileInputStream(new File(
                path,fileNames[i])));
                while((len = bin.read(b))!=-1)
                {
                    zout.write(b,0,len);
                }
                zout.closeEntry();
                bin.close();
            }
            zout.close();
            zin.close();
            String slefFileName = selfFile.getPath();
            selfFile.delete();
            tmpzip.renameTo(new File(slefFileName));
            selfFile=new File(slefFileName);
            aa.close();
            bin.close();
            isChanged=true;
        }
        catch(Exception e)
        {
            if (tmpzip!=null)
            {
                tmpzip.delete();
            }
            throw e;
        }
    }

    /**
     * 将文件重新命名。
     * @param fileName 新文件名
     */
    private void renameTo(String fileName)
    {
        if (fileName==null)
        {
            return;
        }
        File target = new File(fileName);
        if (target.exists())
        {
            return;
        }
        selfFile.renameTo(target);
    }

    /**
     * 比较两个实体是否相等。
     * @param one  压缩实体1
     * @param two  压缩实体2
     * @return  true 相等; false 不相等
     */
    private boolean isSameEntry(ZipEntry one,ZipEntry two)
    {
        return one.getName().equals(two.getName());
    }

    /**
     * 判断一个压缩实体数组是否包含一个压缩实体。
     * @param one  一个压缩实体
     * @param others  压缩实体数组
     * @return true 包含 ; false 不包含
     */
    private boolean isContainEntry(ZipEntry one,ZipEntry[] others)
    {
        for (int i = 0;i<others.length;i++)
        {
            if (isSameEntry(one,others[i]))
            return true;
        }
        return false;
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -