博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
How to append files to a .tar archive using Apache Commons Compress?(转)
阅读量:7197 次
发布时间:2019-06-29

本文共 2232 字,大约阅读时间需要 7 分钟。

 

I created a copy of the tar archive and copied to entire content to it. Then I delete the old tar archive.

public void appendFileInTarArchive(String tarPath, String tarFileName, String file2WriteName, String file2WriteContent) throws AuthenticationException, IOException {    if (tarPath == null || tarFileName == null || tarFileName.isEmpty()) {        LOG.warn("The path or the name of the tar archive is null or empty.");        return;    }    final File tarFile = new File(tarPath, tarFileName);    final File fileToAdd = new File(tarPath, file2WriteName);    FileUtils.write(fileToAdd, file2WriteContent);    if (file2WriteName == null || file2WriteName.isEmpty()) {        LOG.warn("The name of the file to append in the archive is null or empty.");        return;    }    ArchiveStreamFactory asf = new ArchiveStreamFactory();    File tempFile = new File(tarPath, "tmpTar.tar");    tempFile.createNewFile();    try {        FileInputStream fis = new FileInputStream(tarFile);        ArchiveInputStream ais = asf.createArchiveInputStream(ArchiveStreamFactory.TAR, fis);        FileOutputStream fos = new FileOutputStream(tempFile);        ArchiveOutputStream aos = asf.createArchiveOutputStream(ArchiveStreamFactory.TAR, fos);        // copy the existing entries            ArchiveEntry nextEntry;        while ((nextEntry = ais.getNextEntry()) != null) {            aos.putArchiveEntry(nextEntry);            IOUtils.copy(ais, aos);            aos.closeArchiveEntry();        }        // create the new entry        TarArchiveEntry entry = new TarArchiveEntry(file2WriteName);        entry.setSize(fileToAdd.length());        aos.putArchiveEntry(entry);        IOUtils.copy(new FileInputStream(fileToAdd), aos);        aos.closeArchiveEntry();        aos.finish();        ais.close();        aos.close();        // copies the new file over the old        tarFile.delete();        tempFile.renameTo(tarFile);    } catch (ArchiveException e) {        LOG.error(e.getMessage(), e);    } catch (IOException e) {        LOG.error(e.getMessage(), e);    } finally {        FileUtils.deleteQuietly(fileToAdd);    }}

 

 

 

http://stackoverflow.com/questions/12890508/how-to-append-files-to-a-tar-archive-using-apache-commons-compress?rq=1

转载地址:http://ivkum.baihongyu.com/

你可能感兴趣的文章
OSChina 双十一乱弹 ——来自单身狗的哀鸣
查看>>
OSChina 周三乱弹 ——我们职业更好的名字:爱码士
查看>>
左边的项目管理器不见了
查看>>
android 获取唯一标识
查看>>
HTML5 - Server-Sent Events
查看>>
为MySQL授权
查看>>
用 Octave 对音频文件进行基本数学的信号处理
查看>>
看视频是好的学习方法
查看>>
Get last order or items of customer in magento
查看>>
centos7安装redis3.2.1
查看>>
聚合数据Android SDK 天气查询演示示例
查看>>
java冒泡排序法
查看>>
tomcat启停脚本高级版
查看>>
【Microsoft Edge中新的F12开发者工具】
查看>>
java基础第五天
查看>>
提取Fiddler抓包软件的抓到的数据
查看>>
Vsftpd 安全
查看>>
Android开发之下载服务器上的一张图片到本地java代码实现HttpURLConnection
查看>>
安全运维之:网络性能评估工具Iperf
查看>>
sort 排序
查看>>