溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

java程序壓縮和解壓zip文件

發(fā)布時(shí)間:2020-07-01 10:38:17 來源:網(wǎng)絡(luò) 閱讀:443 作者:zhao_anan 欄目:軟件技術(shù)

java程序壓縮和解壓zip文件

需要了解的知識(shí)匯總:
DeflaterOutputStream :壓縮類的基類。
ZipOutputStream :DeflaterOutputStream的一個(gè)子類,把數(shù)據(jù)壓縮成Zip文件格式
GZIPOutputStream :DeflaterOutputStream的一個(gè)子類,把數(shù)據(jù)壓縮成GZip文件格式

InflaterInputStream :解壓縮類的基類
ZipInputStream :InflaterInputStream的一個(gè)子類,能解壓縮Zip格式的數(shù)據(jù)
GZIPInputStream :InflaterInputStream的一個(gè)子類,能解壓縮Zip格式的數(shù)據(jù)

ZipEntry 類:表示 ZIP 文件條目
ZipFile 類:此類用于從 ZIP 文件讀取條目

以zipentry來命名zip內(nèi)文件列表。
以FileInputStream來讀取文件。
以zipoutputstream輸出流進(jìn)行壓縮文件寫入。
邏輯簡單好懂,附件帶有實(shí)例,解壓即可運(yùn)行。
百度網(wǎng)盤實(shí)例鏈接:https://pan.baidu.com/s/1VSs-UqtU_3Lld2H7-1dMMg
提取碼:mcb3

package com.crscd.server.web.cbtc2.filezip;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;

public class FileZip {
/*

  • author:zhao_anan 20190424 zip compress program
    /
    /
    public void zip(ZipOutputStream out, File f, String base, boolean first)
    throws Exception {
    if (first) {
    if (f.isDirectory()) {
    out.putNextEntry(new ZipEntry("/"));
    base = base + f.getName();
    first = false;
    } else
    base = f.getName();
    }
    if (f.isDirectory()) {
    File[] fl = f.listFiles();
    base = base + "/";
    for (int i = 0; i < fl.length; i++) {
    zip(out, fl[i], base + fl[i].getName(), first);
    }
    } else {
    out.putNextEntry(new ZipEntry(base));
    FileInputStream in = new FileInputStream(f);
    int b;
    System.out.println(base);
    while ((b = in.read()) != -1) {
    out.write(b);
    }
    in.close();
    }
    }*/

    /*

  • unzip
    /
    /
    public void unZipFileByOpache(ZipFile zipFile, String unZipRoot)
    throws Exception, IOException {
    Enumeration<?> e = zipFile.entries();
    ZipEntry zipEntry;
    while (e.hasMoreElements()) {
    zipEntry = (ZipEntry) e.nextElement();
    InputStream fis = zipFile.getInputStream(zipEntry);
    if (zipEntry.isDirectory()) {
    } else {
    File file = new File(unZipRoot + File.separator

    • zipEntry.getName());
      System.out.println(unZipRoot + File.separator
    • zipEntry.getName());
      File parentFile = file.getParentFile();
      parentFile.mkdirs();
      FileOutputStream fos = new FileOutputStream(file);
      byte[] b = new byte[1024];
      int len;
      while ((len = fis.read(b, 0, b.length)) != -1) {
      fos.write(b, 0, len);
      }
      fos.close();
      fis.close();
      }
      }
      }

    public void ZipFile(String zipFileName, String inputFileName)
    throws Exception {
    ZipOutputStream out = new ZipOutputStream(new FileOutputStream(
    zipFileName));
    File inputFile = new File(inputFileName);
    zip(out, inputFile, "", true);
    System.out.println("zip done");
    out.close();
    }

    public void unZipFile(String unZipFileName, String unZipPath)
    throws Exception {
    //ZipFile zipFile = new ZipFile(unZipFileName);
    //unZipFileByOpache(zipFile, unZipPath);
    System.out.println("unZip Ok");
    }

    public static void main(String[] args) throws Exception {
    FileZip fileZip = new FileZip();
    // ZipFile("d:/testfile.zip","d:/TTPlayer");
    fileZip.unZipFile("d:/20120206_SLIS_0002_DY2.zip", "d:/");
    }*/

    /**

  • unzip file 20190424
  • @auther: zhao_anan
  • @param unzipRoot
  • */
    public void unZipFile(String zipFilename, String unzipRoot)
    throws Exception {
    BufferedOutputStream bos = null;
    FileInputStream fis = new FileInputStream(zipFilename);
    BufferedInputStream bis = new BufferedInputStream(fis);

    ZipInputStream zis = new ZipInputStream(bis);
    ZipEntry entry = null;
    
    int count = 0;
    byte buf[] = new byte[1024];
    
    while ((entry = zis.getNextEntry()) != null) {
    
        if (entry.isDirectory()) {
    
        } else {
            File file = new File(unzipRoot + File.separator
                    + entry.getName());
            File parentFile = file.getParentFile();
            parentFile.mkdirs();
    
            bos = new BufferedOutputStream(new FileOutputStream(file));
            while ((count = zis.read(buf)) != -1) {
                bos.write(buf, 0, count);
            }
            bos.flush();
            bos.close();
        }
    }
    zis.close();

    }
    /**

  • zip
  • @param zipFilename
  • @param unzipRoot
  • */
    private void zip(ZipOutputStream out, File f, String base, boolean first)
    throws Exception {
    if (first) {
    if (f.isDirectory()) {
    out.putNextEntry(new ZipEntry("/"));
    base = base + f.getName();
    first = false;
    } else
    base = f.getName();
    }
    if (f.isDirectory()) {
    File[] fl = f.listFiles();
    base = base + "/";
    for (int i = 0; i < fl.length; i++) {
    zip(out, fl[i], base + fl[i].getName(), first);
    }
    } else {
    out.putNextEntry(new ZipEntry(base));
    // FileInputStream in = new FileInputStream(f);
    BufferedReader in = new BufferedReader(new InputStreamReader(
    new FileInputStream(f), "ISO8859_1"));
    int b;
    System.out.println(base);
    while ((b = in.read()) != -1) {
    out.write(b);
    }
    in.close();
    }
    }

    public void zipFile(String zipFileName, String inputFileName)
    throws Exception {
    ZipOutputStream out = new ZipOutputStream(new FileOutputStream(
    zipFileName));
    File inputFile = new File(inputFileName);
    zip(out, inputFile, "", true);
    System.out.println("zip done");
    out.close();
    }

    /**

  • zip
  • @param zipFilename
  • @param unzipRoot
  • */
    private void zipList(ZipOutputStream out, String[] fileList, String base)
    throws Exception {
    base = base + "\";
    for (int i = 0; i < fileList.length; i++) {
    //zip(out, , base + fileList[i], first);
    out.putNextEntry(new ZipEntry(fileList[i]));
    BufferedReader in = new BufferedReader(new InputStreamReader(
    new FileInputStream(base + fileList[i]), "ISO8859_1"));
    int b;
    System.out.println(base + fileList[i]);
    while ((b = in.read()) != -1) {
    out.write(b);
    }
    in.close();
    }
    }

    public void zipListFile(String zipFileName, String inputListFiles, String baseurl)
    throws Exception {
    ZipOutputStream out = new ZipOutputStream(new FileOutputStream(
    zipFileName));
    String[] FileList = inputListFiles.split(",");
    zipList(out, FileList, baseurl);
    System.out.println("zip done");
    out.close();
    }

    public static void main(String[] args) {

    //壓縮測(cè)試
    FileZip tFileZip = new FileZip();
    String tinputFileName = "E:\\temp";
    String baseurl = "E:\\temp";
    String tzipFileName = "E:\\temp.zip";
    String inputListFiles = "application-dev.yml,auto_arrange.yml";
    try {
        //tFileZip.zipFile(tzipFileName, tinputFileName);
        tFileZip.zipListFile(tzipFileName, inputListFiles, baseurl);
        //tFileZip.unZipFile(tzipFileName, baseurl + "unzip");
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    }
    }

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI