The tar Ant task has a usecase where absolute paths inside archives are required - allow creation of such entries
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/compress/trunk@807514 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 1c4615a..adcc70b 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -23,6 +23,11 @@
</properties>
<body>
<release version="1.1" date="as in SVN" description="Release 1.1">
+ <action type="add" date="2009-08-25">
+ A new constructor of TarArchiveEntry can create entries with
+ names that start with slashes - the default is to strip
+ leading slashes in order to create relative path names.
+ </action>
<action issue="COMPRESS-83" type="fix" date="2009-08-01">
Delegate all read and write methods in GZip stream in order to
speed up operations.
diff --git a/src/main/java/org/apache/commons/compress/archivers/tar/TarArchiveEntry.java b/src/main/java/org/apache/commons/compress/archivers/tar/TarArchiveEntry.java
index 71ddeb6..a7a3139 100644
--- a/src/main/java/org/apache/commons/compress/archivers/tar/TarArchiveEntry.java
+++ b/src/main/java/org/apache/commons/compress/archivers/tar/TarArchiveEntry.java
@@ -167,9 +167,21 @@
* @param name the entry name
*/
public TarArchiveEntry(String name) {
+ this(name, false);
+ }
+
+ /**
+ * Construct an entry with only a name. This allows the programmer
+ * to construct the entry's header "by hand". File is set to null.
+ *
+ * @param name the entry name
+ * @param preserveLeadingSlashes whether to allow leading slashes
+ * in the name.
+ */
+ public TarArchiveEntry(String name, boolean preserveLeadingSlashes) {
this();
- name = normalizeFileName(name);
+ name = normalizeFileName(name, preserveLeadingSlashes);
boolean isDir = name.endsWith("/");
this.devMajor = 0;
@@ -208,7 +220,7 @@
* @param file The file that the entry represents.
*/
public TarArchiveEntry(File file) {
- this(file, normalizeFileName(file.getPath()));
+ this(file, normalizeFileName(file.getPath(), false));
}
/**
@@ -320,7 +332,7 @@
* @param name This entry's new name.
*/
public void setName(String name) {
- this.name = normalizeFileName(name);
+ this.name = normalizeFileName(name, false);
}
/**
@@ -642,7 +654,8 @@
* Strips Windows' drive letter as well as any leading slashes,
* turns path separators into forward slahes.
*/
- private static String normalizeFileName(String fileName) {
+ private static String normalizeFileName(String fileName,
+ boolean preserveLeadingSlashes) {
String osname = System.getProperty("os.name").toLowerCase(Locale.US);
if (osname != null) {
@@ -674,7 +687,7 @@
// No absolute pathnames
// Windows (and Posix?) paths can start with "\\NetworkDrive\",
// so we loop on starting /'s.
- while (fileName.startsWith("/")) {
+ while (!preserveLeadingSlashes && fileName.startsWith("/")) {
fileName = fileName.substring(1);
}
return fileName;