java getnexttarentry_Java TarEntry.getSize方法代碼示例

这段代码展示了如何在使用Apache的TarEntry类创建TAR归档时处理超过标准长度的文件名。当文件名过长时,它会采用GNU长链接格式,将长文件名作为新的TarEntry写入,确保长文件名能被正确地存档和读取。
摘要由CSDN通过智能技术生成

import org.apache.tools.tar.TarEntry; //導入方法依賴的package包/類

/**

* Put an entry on the output stream. This writes the entry's

* header record and positions the output stream for writing

* the contents of the entry. Once this method is called, the

* stream is ready for calls to write() to write the entry's

* contents. Once the contents are written, closeEntry()

* MUST be called to ensure that all buffered data

* is completely written to the output stream.

*

* @param entry The TarEntry to be written to the archive.

* @throws IOException on error

*/

//@Override

public void putNextEntry(TarEntry entry) throws IOException {

if (entry.getName().length() >= TarConstants.NAMELEN) {

if (longFileMode == LONGFILE_GNU) {

// create a TarEntry for the LongLink, the contents

// of which are the entry's name

TarEntry longLinkEntry = new SFRMTarEntry(TarConstants.GNU_LONGLINK,

TarConstants.LF_GNUTYPE_LONGNAME);

// longLinkEntry.setSize(entry.getName().length() + 1);

byte[] nameBytes = entry.getName().getBytes(SFRMTarUtils.NAME_ENCODING);

longLinkEntry.setSize(nameBytes.length + 1);

putNextEntry(longLinkEntry);

// write(entry.getName().getBytes());

write(nameBytes);

write(0);

closeEntry();

} else if (longFileMode != LONGFILE_TRUNCATE) {

throw new RuntimeException("file name '" + entry.getName()

+ "' is too long ( > "

+ TarConstants.NAMELEN + " bytes)");

}

}

entry.writeEntryHeader(this.recordBuf);

this.buffer.writeRecord(this.recordBuf);

this.currBytes = 0;

if (entry.isDirectory()) {

this.currSize = 0;

} else {

this.currSize = entry.getSize();

}

currName = entry.getName();

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值