在做一个补丁管理系统,要实现一个对文件打包的过程,因为补丁已经是压缩过的,所以打包时不需要再压缩,另一方面没有压缩打包过程也会快很多,发现zip进行打包已经是默认压缩的。但是tar包是可控的,可以设置压缩选项,此处是用java实现,
引入apache的公共组件包
- import org.apache.commons.compress.archivers.ArchiveEntry;
- import org.apache.commons.compress.archivers.ArchiveOutputStream;
- import org.apache.commons.compress.archivers.ArchiveStreamFactory;
- import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
- import org.apache.commons.compress.utils.IOUtils;
下面是实现,带了很多项目内部字段,请大家见谅
- /**
- * 文件打包
- *
- * @return
- * @throws Exception
- */
- public boolean filesToTar(TaskInfo taskInfo) throws Exception
- {
- /* 文件打包 */
- String curreenTimeFlag = getCurrentTimeFlag();
- tarFullName = tarName + curreenTimeFlag;
- tarPath = "k:/packetFolder/" + tarFullName + ".tar";
- try {
- File output = new File(tarPath);
- OutputStream out = new FileOutputStream(output);
- ArchiveOutputStream os = new ArchiveStreamFactory()
- .createArchiveOutputStream("tar", out);
- TarArchiveEntry entry = new TarArchiveEntry("");
- if(addFileToArchiveEntry(os, getPatchInfoMap(taskInfo), taskInfo) == 2)
- {
- return false;
- }
- entry.setUserId(0);
- entry.setGroupId(0);
- entry.setUserName("avalon");
- entry.setGroupName("excalibur");
- entry.setMode(0100000); //设置打包模式
- os.putArchiveEntry(entry);
- os.closeArchiveEntry();
- os.close();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- File file = new File(tarPath);
- file.delete();
- e.printStackTrace();
- return false;
- }
- taskInfo.savePath = tarPath;
- return true;
- }
- /**
- * 多个文件打包的实现(子函数)
- *
- * @return 2:取消打包 1:打包完成
- * @param os
- * @param patchFileMap
- * @param isCanceled 是否取消打包
- * @throws FileNotFoundException
- * @throws IOException
- */
- @SuppressWarnings("rawtypes")
- public int addFileToArchiveEntry(ArchiveOutputStream os,
- Map<String, TarFileOtherInfo> patchFileMap, TaskInfo taskInfo) throws FileNotFoundException,
- IOException {
- int tarSizeProcessing = 0;
- Set keys = patchFileMap.keySet();
- Iterator iterator = keys.iterator();
- while (iterator.hasNext()) {
- if(taskInfo.isCanceled)
- {
- return 2;
- }
- String patchFileId = (String) iterator.next();
- TarFileOtherInfo tarFileOtherInfo;
- tarFileOtherInfo = patchFileMap.get(patchFileId);
- String patchFilePath = tarFileOtherInfo.fullPath;
- String patchFileFullPath = getRootFilepath() + patchFilePath;
- File file = getFile(patchFileFullPath);
- if(file == null)
- return 2;
- String patchAlias;
- patchAlias = splitPatchPath(patchFilePath) + patchFileId + ".exe";
- if(!addArchiveEntry(os, patchAlias, file))
- return 2;
- tarSizeProcessing = tarSizeProcessing + tarFileOtherInfo.fileSize;
- taskInfo.currentSize = tarSizeProcessing;
- }
- File filesql = getFile(sqlLitePath);
- String sqlLite = splitString(sqlLitePath);
- if(!addArchiveEntry(os, sqlLite, filesql))
- return 2;
- return 1;
- }
- /**
- * 单个文件打包的实现
- *
- * @param os
- * @param filename
- * @param infile
- * @throws IOException
- * @throws FileNotFoundException
- */
- public boolean addArchiveEntry(ArchiveOutputStream os, String filename,
- final File infile) {
- ArchiveEntry entry;
- try {
- entry = os.createArchiveEntry(infile, filename);
- os.putArchiveEntry(entry);
- IOUtils.copy(new FileInputStream(infile), os);
- os.closeArchiveEntry();
- return true;
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- return false;
- }
- }
本文出自 “永远的朋友” 博客,请务必保留此出处http://yaocoder.blog.51cto.com/2668309/602120