Author: bodewig Date: Tue Mar 20 20:27:07 2012 New Revision: 1303135 URL: http://svn.apache.org/viewvc?rev=1303135&view=rev Log: allow the encoding to be specified when writing tar archives
Modified: commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/tar/TarArchiveEntry.java commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/tar/TarArchiveOutputStream.java commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/tar/TarUtils.java Modified: commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/tar/TarArchiveEntry.java URL: http://svn.apache.org/viewvc/commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/tar/TarArchiveEntry.java?rev=1303135&r1=1303134&r2=1303135&view=diff ============================================================================== --- commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/tar/TarArchiveEntry.java (original) +++ commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/tar/TarArchiveEntry.java Tue Mar 20 20:27:07 2012 @@ -811,22 +811,34 @@ public class TarArchiveEntry implements * @param outbuf The tar entry header buffer to fill in. */ public void writeEntryHeader(byte[] outbuf) { - writeEntryHeader(outbuf, false); + try { + writeEntryHeader(outbuf, TarUtils.DEFAULT_ENCODING, false); + } catch (IOException ex) { + try { + writeEntryHeader(outbuf, TarUtils.FALLBACK_ENCODING, false); + } catch (IOException ex2) { + // impossible + throw new RuntimeException(ex2); + } + } } /** * Write an entry's header information to a header buffer. * * @param outbuf The tar entry header buffer to fill in. + * @param encoding encoding to use when writing the file name. * @param starMode whether to use the star/GNU tar/BSD tar * extension for numeric fields if their value doesn't fit in the * maximum size of standard tar archives * @since Apache Commons Compress 1.4 */ - public void writeEntryHeader(byte[] outbuf, boolean starMode) { + public void writeEntryHeader(byte[] outbuf, ZipEncoding encoding, + boolean starMode) throws IOException { int offset = 0; - offset = TarUtils.formatNameBytes(name, outbuf, offset, NAMELEN); + offset = TarUtils.formatNameBytes(name, outbuf, offset, NAMELEN, + encoding); offset = writeEntryHeaderField(mode, outbuf, offset, MODELEN, starMode); offset = writeEntryHeaderField(userId, outbuf, offset, UIDLEN, starMode); @@ -843,11 +855,14 @@ public class TarArchiveEntry implements } outbuf[offset++] = linkFlag; - offset = TarUtils.formatNameBytes(linkName, outbuf, offset, NAMELEN); + offset = TarUtils.formatNameBytes(linkName, outbuf, offset, NAMELEN, + encoding); offset = TarUtils.formatNameBytes(magic, outbuf, offset, MAGICLEN); offset = TarUtils.formatNameBytes(version, outbuf, offset, VERSIONLEN); - offset = TarUtils.formatNameBytes(userName, outbuf, offset, UNAMELEN); - offset = TarUtils.formatNameBytes(groupName, outbuf, offset, GNAMELEN); + offset = TarUtils.formatNameBytes(userName, outbuf, offset, UNAMELEN, + encoding); + offset = TarUtils.formatNameBytes(groupName, outbuf, offset, GNAMELEN, + encoding); offset = writeEntryHeaderField(devMajor, outbuf, offset, DEVLEN, starMode); offset = writeEntryHeaderField(devMinor, outbuf, offset, DEVLEN, Modified: commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/tar/TarArchiveOutputStream.java URL: http://svn.apache.org/viewvc/commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/tar/TarArchiveOutputStream.java?rev=1303135&r1=1303134&r2=1303135&view=diff ============================================================================== --- commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/tar/TarArchiveOutputStream.java (original) +++ commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/tar/TarArchiveOutputStream.java Tue Mar 20 20:27:07 2012 @@ -254,7 +254,8 @@ public class TarArchiveOutputStream exte } TarArchiveEntry entry = (TarArchiveEntry) archiveEntry; Map<String, String> paxHeaders = new HashMap<String, String>(); - if (entry.getName().length() >= TarConstants.NAMELEN) { + final byte[] nameBytes = encoding.encode(entry.getName()).array(); + if (nameBytes.length >= TarConstants.NAMELEN) { if (longFileMode == LONGFILE_POSIX) { paxHeaders.put("path", entry.getName()); @@ -264,7 +265,6 @@ public class TarArchiveOutputStream exte TarArchiveEntry longLinkEntry = new TarArchiveEntry(TarConstants.GNU_LONGLINK, TarConstants.LF_GNUTYPE_LONGNAME); - final byte[] nameBytes = ArchiveUtils.toAsciiBytes(entry.getName()); longLinkEntry.setSize(nameBytes.length + 1); // +1 for NUL putArchiveEntry(longLinkEntry); write(nameBytes); @@ -287,7 +287,8 @@ public class TarArchiveOutputStream exte writePaxHeaders(entry.getName(), paxHeaders); } - entry.writeEntryHeader(recordBuf, bigNumberMode == BIGNUMBER_STAR); + entry.writeEntryHeader(recordBuf, encoding, + bigNumberMode == BIGNUMBER_STAR); buffer.writeRecord(recordBuf); currBytes = 0; Modified: commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/tar/TarUtils.java URL: http://svn.apache.org/viewvc/commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/tar/TarUtils.java?rev=1303135&r1=1303134&r2=1303135&view=diff ============================================================================== --- commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/tar/TarUtils.java (original) +++ commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/tar/TarUtils.java Tue Mar 20 20:27:07 2012 @@ -41,7 +41,7 @@ public class TarUtils { * Encapsulates the algorithms used up to Commons Compress 1.3 as * ZipEncoding. */ - private static final ZipEncoding FALLBACK_ENCODING = new ZipEncoding() { + static final ZipEncoding FALLBACK_ENCODING = new ZipEncoding() { public boolean canEncode(String name) { return true; } public ByteBuffer encode(String name) {