压缩

 思路

  • 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());
        }
    }