Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions src/main/java/org/codehaus/plexus/archiver/AbstractArchiver.java
Original file line number Diff line number Diff line change
Expand Up @@ -616,6 +616,23 @@ private static void closeQuietlyIfCloseable(Object resource) {
}
}

/**
* Returns a map of the files that have been added to the archive.
* <p>
* Note: The entry names in the map use platform-specific path separators
* (e.g., backslashes on Windows, forward slashes on Unix). For ZIP archivers,
* the actual archive entries will use forward slashes as required by the ZIP
* specification, but this map returns names as they were added.
* </p>
* <p>
* For ZIP-based archivers (ZipArchiver, JarArchiver, etc.), use the overridden
* implementation which normalizes paths to forward slashes to match the actual
* ZIP entry names.
* </p>
*
* @return A map where keys are entry names and values are the corresponding ArchiveEntry objects.
* @deprecated Use {@link #getResources()} instead.
*/
@Override
@Deprecated
public Map<String, ArchiveEntry> getFiles() {
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/org/codehaus/plexus/archiver/Archiver.java
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,16 @@ void addArchivedFileSet(@Nonnull File archiveFile, String prefix, String[] inclu
ResourceIterator getResources() throws ArchiverException;

/**
* Returns a map of the files that have been added to the archive.
* <p>
* Note: The entry names in the map may use platform-specific path separators
* in the base implementation. However, archive format-specific implementations
* (such as ZIP-based archivers) should normalize paths according to their format
* requirements. For example, ZIP archivers normalize to forward slashes as required
* by the ZIP file specification.
* </p>
*
* @return A map where keys are entry names and values are the corresponding ArchiveEntry objects.
* @deprecated Use {@link #getResources()}
*/
@Deprecated
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,10 @@
import java.nio.file.attribute.FileTime;
import java.util.Calendar;
import java.util.Deque;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Locale;
import java.util.Map;
import java.util.TimeZone;
import java.util.concurrent.ExecutionException;
import java.util.zip.CRC32;
Expand Down Expand Up @@ -564,6 +566,29 @@ protected boolean createEmptyZip(File zipFile) throws ArchiverException {
return true;
}

/**
* Returns a map of the files that have been added to the archive.
* This method is overridden to normalize path separators to forward slashes,
* as required by the ZIP file format specification.
*
* @return A map where keys are entry names with forward slashes as separators,
* and values are the corresponding ArchiveEntry objects.
* @deprecated Use {@link #getResources()} instead.
*/
@Override
@Deprecated
public Map<String, ArchiveEntry> getFiles() {
Map<String, ArchiveEntry> files = super.getFiles();
Map<String, ArchiveEntry> normalizedFiles = new HashMap<>();

for (Map.Entry<String, ArchiveEntry> entry : files.entrySet()) {
String normalizedPath = entry.getKey().replace(File.separatorChar, '/');
normalizedFiles.put(normalizedPath, entry.getValue());
}

return normalizedFiles;
}

/**
* Do any clean up necessary to allow this instance to be used again.
* <p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
import org.apache.commons.compress.archivers.zip.ZipExtraField;
import org.apache.commons.compress.archivers.zip.ZipFile;
import org.apache.commons.io.input.BoundedInputStream;
import org.codehaus.plexus.archiver.ArchiveEntry;
import org.codehaus.plexus.archiver.Archiver;
import org.codehaus.plexus.archiver.ArchiverException;
import org.codehaus.plexus.archiver.BasePlexusArchiverTest;
Expand Down Expand Up @@ -857,4 +858,38 @@ private long toLocalTimeZone(long timestamp) {
return 0L;
}
}

/**
* Test that getFiles() returns entry names with forward slashes (not platform-specific separators)
* as required by the ZIP file specification.
*/
@Test
void testGetFilesReturnsForwardSlashes() throws Exception {
File zipFile = getTestFile("target/output/test-getfiles-slashes.zip");
ZipArchiver archiver = getZipArchiver(zipFile);

// Add files with nested directory structure
File pomFile = new File("pom.xml");
archiver.addFile(pomFile, "dir1/dir2/pom.xml");
archiver.addFile(pomFile, "another/nested/path/file.xml");

// Get the files map BEFORE creating the archive
Map<String, ArchiveEntry> files = archiver.getFiles();

// Verify all entry names use forward slashes
for (String entryName : files.keySet()) {
assertFalse(entryName.contains("\\"), "Entry name should not contain backslashes, but got: " + entryName);
assertTrue(
entryName.contains("/") || !entryName.contains(File.separator),
"Entry name should use forward slashes as separator: " + entryName);
}

// Verify specific entries exist with correct format
assertTrue(files.containsKey("dir1/dir2/pom.xml"), "Should contain dir1/dir2/pom.xml");
assertTrue(files.containsKey("another/nested/path/file.xml"), "Should contain another/nested/path/file.xml");

// Create the archive to ensure it's valid
archiver.createArchive();
assertTrue(zipFile.exists());
}
}
Loading