在java中实现tar,gzip功能

摘要: 在将文件归档时,通常将文件压缩,而程序一般部署在web application 上的,在程序里部署一段程序,就可以实现用JAVA 压缩归档。代码片段, 请查看文章详情 ...

在将文件归档时,通常将文件压缩,而程序一般部署在web application 上的,在程序里部署一段程序,就可以实现用JAVA 压缩归档。

/**
 * Compress (tar.gz) the input file (or directory) to the output file
 * 

* * In the case of a directory all files within the directory (and all nested * directories) will be added to the archive * * @param file The file(s if a directory) to compress * @param output The resulting output file (should end in .tar.gz) * @throws IOException */ public static void compressFile(File file, File output) throws IOException { ArrayList list = new ArrayList(1); list.add(file); compressFiles(list, output); } /** * Compress (tar.gz) the input files to the output file * * @param files The files to compress * @param output The resulting output file (should end in .tar.gz) * @throws IOException */ public static void compressFiles(Collection files, File output) throws IOException { LOG.debug("Compressing "+files.size() + " to "+output.getAbsoluteFile()); // Create the output stream for the output file FileOutputStream fos = new FileOutputStream(output); // Wrap the output file stream in streams that will tar and gzip everything TarArchiveOutputStream taos = new TarArchiveOutputStream( new GZIPOutputStream(new BufferedOutputStream(fos))); // TAR has an 8 gig file limit by default, this gets around that taos.setBigNumberMode(TarArchiveOutputStream.BIGNUMBER_STAR); // to get past the 8 gig limit // TAR originally didn't support long file names, so enable the support for it taos.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU); // Get to putting all the files in the compressed output file for (File f : files) { addFilesToCompression(taos, f, "."); } // Close everything up taos.close(); fos.close(); } /** * Does the work of compression and going recursive for nested directories *

* * Borrowed heavily from http://www.thoughtspark.org/node/53 * * @param taos The archive * @param file The file to add to the archive * @param dir The directory that should serve as the parent directory in the archivew * @throws IOException */ private static void addFilesToCompression(TarArchiveOutputStream taos, File file, String dir) throws IOException { // Create an entry for the file taos.putArchiveEntry(new TarArchiveEntry(file, dir+FILE_SEPARATOR+file.getName())); if (file.isFile()) { // Add the file to the archive BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file)); IOUtils.copy(bis, taos); taos.closeArchiveEntry(); bis.close(); } else if (file.isDirectory()) { // close the archive entry taos.closeArchiveEntry(); // go through all the files in the directory and using recursion, add them to the archive for (File childFile : file.listFiles()) { addFilesToCompression(taos, childFile, file.getName()); } } }

上一篇: VPS CENTOS 上配置python,mysql,nginx,uwsgi,django全过程
下一篇: 好久没更新博客了,因为做另外一个python CMS.
 评论 ( What Do You Think )
名称
邮箱
网址
评论
验证
   
 

 


  • 微信公众号

  • 我的微信

站点声明:

1、一号门博客CMS,由Python, MySQL, Nginx, Wsgi 强力驱动

2、部分文章或者资源来源于互联网, 有时候很难判断是否侵权, 若有侵权, 请联系邮箱:summer@yihaomen.com, 同时欢迎大家注册用户,主动发布无版权争议的 文章/资源.

3、鄂ICP备14001754号-3, 鄂公网安备 42280202422812号