无压缩的打包文件

简介:

 在做一个补丁管理系统,要实现一个对文件打包的过程,因为补丁已经是压缩过的,所以打包时不需要再压缩,另一方面没有压缩打包过程也会快很多,发现zip进行打包已经是默认压缩的。但是tar包是可控的,可以设置压缩选项,此处是用java实现,

引入apache的公共组件包

 

 
  1. import org.apache.commons.compress.archivers.ArchiveEntry; 
  2. import org.apache.commons.compress.archivers.ArchiveOutputStream; 
  3. import org.apache.commons.compress.archivers.ArchiveStreamFactory; 
  4. import org.apache.commons.compress.archivers.tar.TarArchiveEntry; 
  5. import org.apache.commons.compress.utils.IOUtils; 

下面是实现,带了很多项目内部字段,请大家见谅 

 

 
  1. /** 
  2.      * 文件打包 
  3.      *  
  4.      * @return 
  5.      * @throws Exception 
  6.      */ 
  7.  
  8.  
  9.     public boolean filesToTar(TaskInfo taskInfo) throws Exception 
  10.     { 
  11.         /* 文件打包 */ 
  12.         String curreenTimeFlag =  getCurrentTimeFlag(); 
  13.         tarFullName = tarName + curreenTimeFlag; 
  14.  
  15.         tarPath = "k:/packetFolder/" + tarFullName + ".tar"
  16.  
  17.         try { 
  18.             File output = new File(tarPath); 
  19.             OutputStream out = new FileOutputStream(output); 
  20.             ArchiveOutputStream os = new ArchiveStreamFactory() 
  21.                     .createArchiveOutputStream("tar", out); 
  22.             TarArchiveEntry entry = new TarArchiveEntry(""); 
  23.             if(addFileToArchiveEntry(os, getPatchInfoMap(taskInfo), taskInfo) == 2
  24.             { 
  25.                 return false
  26.             } 
  27.              
  28.             entry.setUserId(0); 
  29.             entry.setGroupId(0); 
  30.             entry.setUserName("avalon"); 
  31.             entry.setGroupName("excalibur"); 
  32.             entry.setMode(0100000);    //设置打包模式 
  33.             os.putArchiveEntry(entry); 
  34.             os.closeArchiveEntry(); 
  35.             os.close(); 
  36.         } catch (IOException e) { 
  37.             // TODO Auto-generated catch block 
  38.             File file = new File(tarPath); 
  39.             file.delete(); 
  40.             e.printStackTrace(); 
  41.             return false
  42.         } 
  43.         taskInfo.savePath = tarPath; 
  44.         return true
  45.     } 
  46.  
 
  1. /** 
  2.      * 多个文件打包的实现(子函数) 
  3.      *  
  4.      * @return 2:取消打包  1:打包完成 
  5.      * @param os 
  6.      * @param patchFileMap 
  7.      * @param isCanceled     是否取消打包 
  8.      * @throws FileNotFoundException 
  9.      * @throws IOException 
  10.      */ 
  11.     @SuppressWarnings("rawtypes"
  12.     public int addFileToArchiveEntry(ArchiveOutputStream os, 
  13.             Map<String, TarFileOtherInfo> patchFileMap, TaskInfo taskInfo) throws FileNotFoundException, 
  14.             IOException { 
  15.         int tarSizeProcessing = 0
  16.         Set keys = patchFileMap.keySet(); 
  17.         Iterator iterator = keys.iterator(); 
  18.         while (iterator.hasNext()) { 
  19.             if(taskInfo.isCanceled) 
  20.             { 
  21.                 return 2
  22.             } 
  23.             String patchFileId = (String) iterator.next(); 
  24.             TarFileOtherInfo   tarFileOtherInfo;  
  25.             tarFileOtherInfo = patchFileMap.get(patchFileId); 
  26.             String patchFilePath = tarFileOtherInfo.fullPath; 
  27.             String patchFileFullPath = getRootFilepath() + patchFilePath; 
  28.             File file = getFile(patchFileFullPath); 
  29.             if(file == null
  30.                 return 2
  31.             String patchAlias; 
  32.             patchAlias = splitPatchPath(patchFilePath) + patchFileId + ".exe"
  33.             if(!addArchiveEntry(os, patchAlias, file)) 
  34.                 return 2
  35.             tarSizeProcessing = tarSizeProcessing + tarFileOtherInfo.fileSize; 
  36.             taskInfo.currentSize = tarSizeProcessing; 
  37.         } 
  38.         File filesql = getFile(sqlLitePath); 
  39.         String sqlLite = splitString(sqlLitePath); 
  40.         if(!addArchiveEntry(os, sqlLite, filesql)) 
  41.             return 2
  42.         return 1
  43.     } 
  44.  
  45.     /** 
  46.      * 单个文件打包的实现 
  47.      *  
  48.      * @param os 
  49.      * @param filename 
  50.      * @param infile 
  51.      * @throws IOException 
  52.      * @throws FileNotFoundException 
  53.      */ 
  54.     public boolean addArchiveEntry(ArchiveOutputStream os, String filename, 
  55.             final File infile) { 
  56.         ArchiveEntry entry; 
  57.         try { 
  58.             entry = os.createArchiveEntry(infile, filename); 
  59.             os.putArchiveEntry(entry); 
  60.             IOUtils.copy(new FileInputStream(infile), os); 
  61.             os.closeArchiveEntry(); 
  62.             return true
  63.         } catch (IOException e) { 
  64.             // TODO Auto-generated catch block 
  65.             e.printStackTrace(); 
  66.             return false
  67.         } 
  68.          
  69.     } 

 本文转自永远的朋友博客51CTO博客,原文链接http://blog.51cto.com/yaocoder/602120如需转载请自行联系原作者


yaocoder

相关文章
|
29天前
|
Linux
linux命令行打包、压缩及解压缩
linux命令行打包、压缩及解压缩
8 0
|
5月前
|
Java
Java实现zip文件压缩:单个文件、文件夹以及文件和文件夹的压缩
Java实现zip文件压缩:单个文件、文件夹以及文件和文件夹的压缩
|
计算机视觉