Java將文本文件壓縮為tar.gz


  • 壓縮

    •  思路

      •  准備輸出流
        • FileOutputStream
        • BufferedOutputStream
        • TarOutputStream
        • GZIPOutputStream
      •  准備輸入流  
        • FileInputStream
        • BufferedInputStream
      •  將文件打包為tar  
      •  在將打包后的tar文件壓縮  
    •  代碼

      

    /**
     * @功能描述 壓縮tar.gz 文件
     * @param resourceList 源文件集合
     * @param outPath 目標文件
     * @return 返回壓縮結果
     * @throws Exception
     */
    @PrintRunTime(function="壓縮tar.gz文件")
    public static void packet(List<File> resourceList, String outPath) throws Exception {
            //1. 參數驗證, 初始化輸出路徑
            if(resourceList == null || resourceList.size() < 1 || !Verify.isEmpty(outPath)){
                throw new ServiceException("文件壓縮執行異常, 非法參數!");
            }
            long startTime = System.currentTimeMillis();
            // 2. 迭代源文件集合, 將文件打包為Tar
            try (FileOutputStream fileOutputStream = new FileOutputStream(outPath+".tmp");
                    BufferedOutputStream bufferedOutput = new BufferedOutputStream(fileOutputStream);
                    TarOutputStream tarOutputStream = new TarOutputStream(bufferedOutput);) {
                for (File resourceFile : resourceList) {
                    if(!resourceFile.isFile()){
                        continue;
                    }
                    try(FileInputStream fileInputStream = new FileInputStream(resourceFile);
                            BufferedInputStream bufferedInput = new BufferedInputStream(fileInputStream);){
                        TarEntry entry = new TarEntry(new File(resourceFile.getName()));
                        entry.setSize(resourceFile.length());
                        tarOutputStream.putNextEntry(entry);
                        IOUtils.copy(bufferedInput, tarOutputStream);
                    } catch (Exception e) {
                        throw new ServiceException("文件["+resourceFile+"]壓縮執行異常, 嵌套異常: \n" + e.toString());
                    }finally {
                        tarOutputStream.closeEntry();
                    }
                }
            } catch (Exception e) {
                Files.delete(Paths.get(outPath+".tmp"));
                throw new ServiceException("文件壓縮至["+outPath+"]執行異常, 嵌套異常: \n" + e.toString());
            }
            //3. 讀取打包好的Tar臨時文件文件, 使用GZIP方式壓縮
            try (FileInputStream fileInputStream = new FileInputStream(outPath+".tmp");
                    BufferedInputStream bufferedInput = new BufferedInputStream(fileInputStream);
                    FileOutputStream fileOutputStream = new FileOutputStream(outPath);
                    GZIPOutputStream gzipOutputStream = new GZIPOutputStream(fileOutputStream);
                    BufferedOutputStream bufferedOutput = new BufferedOutputStream(gzipOutputStream);
                    ) {
                byte[] cache = new byte[1024];
                for (int index = bufferedInput.read(cache); index != -1; index = bufferedInput.read(cache)) {
                    bufferedOutput.write(cache,0,index);
                }
                long endTime = System.currentTimeMillis();
                Log.info("文件["+outPath+"]壓縮執行完畢, 耗時:" + (endTime - startTime) + "ms");
            } catch (Exception e) {
                throw new ServiceException("文件壓縮至["+outPath+"]執行異常, 嵌套異常: \n" + e.toString());
            }finally {
                Files.delete(Paths.get(outPath+".tmp"));
            }
        }

 

 

  • 解壓

    •   思路

      •  准備輸入流
        •  FileInputStream
        •  BufferedInputStream
        •  GZIPInputStream
        •  TarInputStream
      •  准備輸出流
        •  FileOutputStream
      •  將文件轉換為TarInputStream流對象, 輸出即可
    •   代碼

          /**
           * @功能描述 解壓tar.gz文件
           * @param targzFile tar.gz壓縮文件
           * @param outPath 存放解壓后文件的目錄
           * @return 返回結果
           * @throws ServiceException 
           */
          public static void unpack(File targzFile, String outPath) throws ServiceException {
              //1. 驗證參數, 初始化輸出路徑
              if(targzFile == null || !targzFile.isFile() || !Verify.isEmpty(outPath) || PathHandler.initPath(outPath) == null){
                  throw new ServiceException("文件解壓縮執行異常, 非法參數!");
              }
              long startTime = System.currentTimeMillis();
              //2. 讀取tar.gz文件轉換為tar文件
              try (FileInputStream fileInputStream = new FileInputStream(targzFile);
                      BufferedInputStream bins = new BufferedInputStream(fileInputStream);
                      GZIPInputStream gzipInputStream = new GZIPInputStream(bins);
                      TarInputStream tarIn = new TarInputStream(gzipInputStream, 1024 * 2)) {
                  //3. 迭代tar文件集合, 解壓文件
                  for (TarEntry entry = tarIn.getNextEntry(); entry != null; entry = tarIn.getNextEntry()){
                      File targetFileName = new File(outPath + "/" + entry.getName());
                      IOUtils.copy(tarIn, new FileOutputStream(targetFileName));
                  }
                  long endTime = System.currentTimeMillis();
                  Log.info("文件["+targzFile+"]解壓執行完畢, 耗時:" + (endTime - startTime) + "ms");
              } catch (Exception e) {
                  throw new ServiceException("[" + targzFile + "] 解壓執行異常, " + e.toString());
              }
          }

       


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM