Java中ZIP文件压缩和解压

putNextEntry

使用JAVA JDK看到的英文描述是这样的:

Begins writing a new ZIP file entry and positions the stream to the start of the entry data. Closes the current entry if still active. The default compression method will be used if no compression method was specified for the entry, and the current time will be used if the entry has no set modification time.
 
大致的意思是:
开始写入一个新的ZIP文件条目,并把ZipOutputStream流定位到条目数据的起始位置。
如果当前的条目还是有效的话,关闭当前的条目。
如果该条目的压缩方法没有被指定,则会使用默认的压缩方法。
如果没有设置修改时间的话,会使用当前时间。
 

public static void main(String[] args) throws IOException, ParseException {
    try {
        zip("E:\\\\ind.zip", "E:\\\\ind"); // 压缩
        unZipFiles("E:\\ind.zip", "E:\\");//解压
    } catch (Exception e) {
        e.printStackTrace();
    }

}

private static void zip(String zipFileName, String OutFilePath) throws Exception {
    File file = new File(OutFilePath);
    ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipFileName));
    out.setEncoding("GBK");//设置文件名编码方式
    BufferedOutputStream bo = new BufferedOutputStream(out);
    diGuizip(out, bo, file, file.getName());
    bo.close();
    out.close();
}

private static void diGuizip(ZipOutputStream zipOut, BufferedOutputStream bo, File file, String path) throws IOException {
    if (file.isDirectory()) { // 判断是否是文件夹
        File[] files = file.listFiles();// 获取目录下的文件和文件夹
        if (files.length == 0) zipOut.putNextEntry(new ZipEntry(path + "/"));// 如果是空目录则只创建文件夹
        for (int i = 0; i < files.length; i++) {
            diGuizip(zipOut, bo, files[i], path + "/" + files[i].getName());
        }
    } else {
        zipOut.putNextEntry(new ZipEntry(path));// 创建zip压缩进入点base(开启着陆点)
        BufferedInputStream in = new BufferedInputStream(new FileInputStream(file));
        int ii;
        while ((ii = in.read()) != -1) {
            bo.write(ii);
        }
        bo.flush();
        in.close();
    }
}

public static void unZipFiles(String zipPath, String descDir) throws IOException {
    File pathFile = new File(descDir);
    if (!pathFile.exists()) {
        pathFile.mkdirs();
    }
    Charset cs = Charset.forName("GBK");
    ZipFile zip = new ZipFile(zipPath, cs);
    for (Enumeration entries = zip.entries(); entries.hasMoreElements(); ) {
        ZipEntry entry = (ZipEntry) entries.nextElement();
        System.out.println(entry.getName());
        String zipEntryName = entry.getName();
        InputStream in = zip.getInputStream(entry);
        String outPath = (descDir + "/" + zipEntryName).replaceAll("\\*", "/");
        //判断路径是否存在,不存在则创建文件路径
        File file = new File(outPath.substring(0, outPath.lastIndexOf('/')));
        if (!file.exists()) {
            file.mkdirs();
        }
        //判断文件全路径是否为文件夹,如果是上面已经上传,不需要解压
        if (new File(outPath).isDirectory()) {
            continue;
        }
        OutputStream out = new FileOutputStream(outPath);
        byte[] buf1 = new byte[1024];
        int len;
        while ((len = in.read(buf1)) > 0) {
            out.write(buf1, 0, len);
        }
        in.close();
        out.close();
    }
}

 

 

 

发布者:songJian   点击数:550   发布时间:2017-06-09 01:08:37   更新时间:2021-05-08 11:26:45
正在加载评论...
相关文章