日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

java代码实现压缩文件.gz格式,解压后无后缀名问题

發(fā)布時(shí)間:2023/12/20 编程问答 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java代码实现压缩文件.gz格式,解压后无后缀名问题 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

java代碼實(shí)現(xiàn)壓縮文件.gz格式,解壓后無(wú)后綴名問題

package com.ctid.cps.util.gzipUtil;

import com.ctid.util.file.FileUtil;
import com.ctid.util.file.GZip;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;

/**

  • GZIP工具

  • @author
    */
    public class GzipUtils {

    public static final int BUFFER = 1024;
    public static final String EXT = “.gz”;

    /**

    • 數(shù)據(jù)壓縮

    • @param data

    • @return

    • @throws Exception
      */
      public static byte[] compress(byte[] data) throws Exception {
      ByteArrayInputStream bais = new ByteArrayInputStream(data);
      ByteArrayOutputStream baos = new ByteArrayOutputStream();

      // 壓縮
      compress(bais, baos);

      byte[] output = baos.toByteArray();

      baos.flush();
      baos.close();

      bais.close();

      return output;
      }

    /**

    • 文件壓縮
    • @param file
    • @throws Exception
      */
      public static void compress(File file) throws Exception {
      compress(file, true);
      }

    /**

    • 文件壓縮

    • @param file

    • @param delete 是否刪除原始文件

    • @throws Exception
      */
      public static void compress(File file, boolean delete) throws Exception {
      FileInputStream fis = new FileInputStream(file);
      FileOutputStream fos = new FileOutputStream(file.getPath() + EXT);

      compress(fis, fos);

      fis.close();
      fos.flush();
      fos.close();

      if (delete) {
      file.delete();
      }
      }

    /**

    • 數(shù)據(jù)壓縮

    • @param is

    • @param os

    • @throws Exception
      */
      public static void compress(InputStream is, OutputStream os)
      throws Exception {

      GZIPOutputStream gos = new GZIPOutputStream(os);

      int count;
      byte data[] = new byte[BUFFER];
      while ((count = is.read(data, 0, BUFFER)) != -1) {
      gos.write(data, 0, count);
      }

      gos.finish();

      gos.flush();
      gos.close();
      }

    /**

    • 文件壓縮
    • @param path
    • @throws Exception
      */
      public static void compress(String path) throws Exception {
      compress(path, true);
      }

    /**

    • 文件壓縮
    • @param path
    • @param delete 是否刪除原始文件
    • @throws Exception
      */
      public static void compress(String path, boolean delete) throws Exception {
      File file = new File(path);
      compress(file, delete);
      }

    /**

    • 文件壓縮
    • @param
    • @param
    • @throws Exception
      */
      public static void compress(String inputFileName, String outputFileName)
      throws Exception {
      FileInputStream inputFile = new FileInputStream(inputFileName);
      FileOutputStream outputFile = new FileOutputStream(outputFileName);
      compress(inputFile, outputFile);
      inputFile.close();
      outputFile.flush();
      outputFile.close();
      }

    /**

    • 數(shù)據(jù)解壓縮

    • @param data

    • @return

    • @throws Exception
      */
      public static byte[] decompress(byte[] data) throws Exception {
      ByteArrayInputStream bais = new ByteArrayInputStream(data);
      ByteArrayOutputStream baos = new ByteArrayOutputStream();

      // 解壓縮

      decompress(bais, baos);

      data = baos.toByteArray();

      baos.flush();
      baos.close();

      bais.close();

      return data;
      }

    /**

    • 文件解壓縮
    • @param file
    • @throws Exception
      */
      public static void decompress(File file) throws Exception {
      decompress(file, true, null);
      }

    /**

    • 文件解壓縮

    • @param file 需要解壓的文件

    • @param delete 是否刪除原始文件

    • @param outPath 解壓文件的輸出路徑

    • @throws Exception
      */
      public static void decompress(File file, boolean delete, String outPath)
      throws Exception {
      FileInputStream fis = new FileInputStream(file);
      FileOutputStream fos = null;
      if (outPath == null || outPath == “”) {
      fos = new FileOutputStream(file.getPath().replace(EXT, “”));
      } else {
      File files = new File(outPath);
      //判斷文件是否存在,不存在,則創(chuàng)建
      FileUtil.mkDir(files);//此處調(diào)用了遞歸創(chuàng)建文件夾,沒有寫出,網(wǎng)上很多
      //文件輸出流參數(shù)中,需要指定文件解壓后的文件名,這里,用文件的原名稱
      fos = new FileOutputStream(outPath + File.separator
      + file.getName().replace(EXT, “”));
      }

      decompress(fis, fos);
      fis.close();
      fos.flush();
      fos.close();

      if (delete) {
      file.delete();
      }
      }

    /**

    • 文件解壓縮
    • @param
    • @param
    • @throws Exception
      */
      public static void decompress(String inputFileName, String outputFileName)
      throws Exception {
      FileInputStream inputFile = new FileInputStream(inputFileName);
      FileOutputStream outputFile = new FileOutputStream(outputFileName);
      decompress(inputFile, outputFile);
      inputFile.close();
      outputFile.flush();
      outputFile.close();
      }

    /**

    • 數(shù)據(jù)解壓縮

    • @param is

    • @param os

    • @throws Exception
      */
      public static void decompress(InputStream is, OutputStream os)
      throws Exception {
      GZIPInputStream gis = new GZIPInputStream(is);
      //GZIPInputStream gis = new GZIPInputStream(new BufferedInputStream(is));
      int count;
      byte data[] = new byte[BUFFER];
      while ((count = gis.read(data, 0, BUFFER)) != -1) {
      os.write(data, 0, count);
      }

      gis.close();
      }

    /**

    • 文件解壓縮
    • @param path
    • @throws Exception
      */
      public static void decompress(String path) throws Exception {
      decompress(path, true, null);
      }

    /**

    • 文件解壓縮(解壓?jiǎn)蝹€(gè)文件)
    • @param path 需要解壓的文件路徑(包含文件名稱)
    • @param delete 是否刪除原始文件(true:刪除;false:保留)
    • @param outPath 解壓后文件的輸出路徑,如果該參數(shù)的值為 null,則輸出解壓文件到當(dāng)前文件夾
    • @throws Exception
      */
      public static void decompress(String path, boolean delete, String outPath)
      throws Exception {
      File file = new File(path);
      decompress(file, delete, outPath);
      }

    //測(cè)試,通過遍歷,解壓一個(gè)文件夾中的所有文件
    /* public static void main(String[] args) throws Exception {
    long startTime = System.currentTimeMillis();// 記錄開始時(shí)間
    String path = “D:\compreFile\test2”;
    File file = new File(path);
    String files[] = file.list();
    System.out.println(“共 " + files.length + " 個(gè)文件”);
    Thread.sleep(2000);
    int num = 0;
    List list = new ArrayList();
    for (int x = 0; x < files.length; x++) {
    try {
    //調(diào)用解壓方法
    decompress(path + “\” + files[x], false, “D:\compreFile\test2”);
    //GZip.unpackTarGZ(file,“D:\compreFile\test2”);
    } catch (Exception e) {
    list.add(files[x]);
    continue;
    }
    ++num;
    System.out.println(“第 " + num + " 個(gè)文件.tar.gz解壓成功!!”);
    }
    // String tarPath = “D:\compreFile\test2\”;
    // File fileTar = new File(tarPath);
    // String filesTar[] = fileTar.list();
    // int nber = 0;
    // for (int j = 0; j < filesTar.length; j++) {
    // try {
    // //調(diào)用解壓方法
    // tarToFile.uncompress(tarPath + filesTar[j], “D:\compreFile\test”);
    // } catch (Exception e) {
    // list.add(files[j]);
    // continue;
    // }
    // ++nber;
    // System.out.println(“第 " + nber + " 個(gè)文件.tar解壓成功!!”);
    // }
    for(int x=0;x<list.size();x++){
    System.out.println(list.get(x).toString());
    }
    System.out.println("問題文件 " + list.size() + " 個(gè) ");

    public static void main(String[] args) throws Exception {
    TarToFile tarToFile = new TarToFile();
    long startTime = System.currentTimeMillis();// 記錄開始時(shí)間
    String path = “D:\JAVA壓縮解壓\”;
    File file = new File(path);
    String files[] = file.list();
    System.out.println(“共 " + files.length + " 個(gè)文件”);
    Thread.sleep(2000);
    int num = 0;
    List list = new ArrayList();
    for (int x = 0; x < files.length; x++) {
    try {
    //調(diào)用壓縮方法
    compress(path + files[x], “D:\compreFile\test2\ceshi1” + num + “.gz”);
    System.out.println(path + files[x]);
    } catch (Exception e) {
    System.out.println(e);
    list.add(files[x]);
    continue;
    }
    ++num;
    System.out.println(“第 " + num + " 個(gè)文件壓縮成功!!”);
    }
    System.out.println("問題文件 " + list.size() + " 個(gè) ");
    long endTime = System.currentTimeMillis();// 記錄結(jié)束時(shí)間
    float excTime = (float) (endTime - startTime) / 1000;
    System.out.println(“執(zhí)行時(shí)間:” + excTime + “s”);
    System.out.println(“完成!!”);
    }
    }

這是我的代碼,找了一個(gè)小時(shí)的問題,為什么解壓以后無(wú)后綴,最終是發(fā)現(xiàn)壓縮的時(shí)候在.gz 前面加上文件后綴,比如你要壓縮一個(gè) .java文件
( //調(diào)用壓縮方法的時(shí)候
compress(path + files[x], “D:\compreFile\test2\ceshi1” + num + “.java.gz”);)//把文件后綴得加上去

總結(jié)

以上是生活随笔為你收集整理的java代码实现压缩文件.gz格式,解压后无后缀名问题的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。