Skip to content

Commit a2cb2e1

Browse files
Merge pull request #41 from ChristianSchulte/master
Support the Info-ZIP Unicode Path Extra Field.
2 parents 61bd142 + 2ecffa3 commit a2cb2e1

File tree

6 files changed

+134
-56
lines changed

6 files changed

+134
-56
lines changed

pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151
<dependency>
5252
<groupId>org.codehaus.plexus</groupId>
5353
<artifactId>plexus-utils</artifactId>
54-
<version>3.0.23</version>
54+
<version>3.0.24</version>
5555
</dependency>
5656
<dependency>
5757
<groupId>org.codehaus.plexus</groupId>

src/main/java/org/codehaus/plexus/archiver/jar/JdkManifestFactory.java

+23-15
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
* limitations under the License.
1717
*/
1818

19-
import java.io.InputStream;
19+
import java.io.IOException;
2020
import java.util.Map;
2121
import java.util.Properties;
2222
import java.util.jar.Attributes;
@@ -33,25 +33,33 @@ class JdkManifestFactory
3333
public static java.util.jar.Manifest getDefaultManifest()
3434
throws ArchiverException
3535
{
36-
final java.util.jar.Manifest defaultManifest = new java.util.jar.Manifest();
37-
defaultManifest.getMainAttributes().putValue( "Manifest-Version", "1.0" );
36+
try
37+
{
38+
final java.util.jar.Manifest defaultManifest = new java.util.jar.Manifest();
39+
defaultManifest.getMainAttributes().putValue( "Manifest-Version", "1.0" );
3840

39-
String createdBy = "Plexus Archiver";
41+
String createdBy = "Plexus Archiver";
4042

41-
InputStream inputStream = JdkManifestFactory.class.getResourceAsStream( "/META-INF/"
42-
+ "maven/org.codehaus.plexus/plexus-archiver/pom.properties" );
43-
Properties properties = PropertyUtils.loadProperties( inputStream );
44-
if ( properties != null )
45-
{
46-
String plexusArchiverVersion = properties.getProperty( "version" );
47-
if ( plexusArchiverVersion != null )
43+
final Properties properties = PropertyUtils.loadProperties( JdkManifestFactory.class.getResourceAsStream(
44+
"/META-INF/maven/org.codehaus.plexus/plexus-archiver/pom.properties" ) );
45+
46+
if ( properties != null )
4847
{
49-
createdBy += " " + plexusArchiverVersion;
48+
String plexusArchiverVersion = properties.getProperty( "version" );
49+
if ( plexusArchiverVersion != null )
50+
{
51+
createdBy += " " + plexusArchiverVersion;
52+
}
5053
}
51-
}
52-
defaultManifest.getMainAttributes().putValue( "Created-By", createdBy );
5354

54-
return defaultManifest;
55+
defaultManifest.getMainAttributes().putValue( "Created-By", createdBy );
56+
57+
return defaultManifest;
58+
}
59+
catch ( final IOException e )
60+
{
61+
throw new ArchiverException( "Failure reading default manifest.", e );
62+
}
5563
}
5664

5765
public static void merge( java.util.jar.Manifest target, java.util.jar.Manifest other, boolean overwriteMain )

src/main/java/org/codehaus/plexus/archiver/jar/Manifest.java

+21-14
Original file line numberDiff line numberDiff line change
@@ -735,25 +735,32 @@ public Iterator<String> iterator()
735735
public static Manifest getDefaultManifest()
736736
throws ArchiverException
737737
{
738-
final Manifest defaultManifest = new Manifest();
739-
defaultManifest.getMainAttributes().putValue( "Manifest-Version", "1.0" );
738+
try
739+
{
740+
final Manifest defaultManifest = new Manifest();
741+
defaultManifest.getMainAttributes().putValue( "Manifest-Version", "1.0" );
740742

741-
String createdBy = "Plexus Archiver";
743+
String createdBy = "Plexus Archiver";
742744

743-
InputStream inputStream = Manifest.class.getResourceAsStream( "/META-INF/"
744-
+ "maven/org.codehaus.plexus/plexus-archiver/pom.properties" );
745-
Properties properties = PropertyUtils.loadProperties( inputStream );
746-
if ( properties != null )
747-
{
748-
String plexusArchiverVersion = properties.getProperty( "version" );
749-
if ( plexusArchiverVersion != null )
745+
final Properties properties = PropertyUtils.loadProperties( Manifest.class.getResourceAsStream(
746+
"/META-INF/maven/org.codehaus.plexus/plexus-archiver/pom.properties" ) );
747+
748+
if ( properties != null )
750749
{
751-
createdBy += " " + plexusArchiverVersion;
750+
String plexusArchiverVersion = properties.getProperty( "version" );
751+
if ( plexusArchiverVersion != null )
752+
{
753+
createdBy += " " + plexusArchiverVersion;
754+
}
752755
}
753-
}
754-
defaultManifest.getMainAttributes().putValue( "Created-By", createdBy );
756+
defaultManifest.getMainAttributes().putValue( "Created-By", createdBy );
755757

756-
return defaultManifest;
758+
return defaultManifest;
759+
}
760+
catch ( final IOException e )
761+
{
762+
throw new ArchiverException( "Failure reading default manifest.", e );
763+
}
757764
}
758765

759766
/**

src/main/java/org/codehaus/plexus/archiver/zip/AbstractZipArchiver.java

+42-3
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import org.apache.commons.compress.archivers.zip.ZipEncoding;
2323
import org.apache.commons.compress.archivers.zip.ZipEncodingHelper;
2424
import org.apache.commons.compress.parallel.InputStreamSupplier;
25+
import org.apache.commons.compress.utils.Charsets;
2526
import org.codehaus.plexus.archiver.AbstractArchiver;
2627
import org.codehaus.plexus.archiver.ArchiveEntry;
2728
import org.codehaus.plexus.archiver.Archiver;
@@ -41,6 +42,7 @@
4142
import java.io.InputStream;
4243
import java.io.OutputStream;
4344
import java.io.SequenceInputStream;
45+
import java.nio.charset.Charset;
4446
import java.util.Hashtable;
4547
import java.util.Stack;
4648
import java.util.concurrent.ExecutionException;
@@ -208,7 +210,6 @@ public boolean isFilesonly()
208210
return doFilesonly;
209211
}
210212

211-
212213
protected void execute()
213214
throws ArchiverException, IOException
214215
{
@@ -315,8 +316,7 @@ private void createArchiveMain()
315316
zipArchiveOutputStream =
316317
new ZipArchiveOutputStream( bufferedOutputStream( fileOutputStream( zipFile, "zip" ) ) );
317318
zipArchiveOutputStream.setEncoding( encoding );
318-
zipArchiveOutputStream.setCreateUnicodeExtraFields(
319-
ZipArchiveOutputStream.UnicodeExtraFieldPolicy.NOT_ENCODEABLE );
319+
zipArchiveOutputStream.setCreateUnicodeExtraFields( this.getUnicodeExtraFieldPolicy() );
320320
zipArchiveOutputStream.setMethod(
321321
doCompress ? ZipArchiveOutputStream.DEFLATED : ZipArchiveOutputStream.STORED );
322322

@@ -339,6 +339,45 @@ private void createArchiveMain()
339339
success = true;
340340
}
341341

342+
/**
343+
* Gets the {@code UnicodeExtraFieldPolicy} to apply.
344+
*
345+
* @return {@link ZipArchiveOutputStream.UnicodeExtraFieldPolicy.NOT_ENCODEABLE}, if the effective encoding is
346+
* UTF-8; {@link ZipArchiveOutputStream.UnicodeExtraFieldPolicy.ALWAYS}, if the effective encoding is not
347+
* UTF-8.
348+
*
349+
* @see #getEncoding()
350+
*/
351+
private ZipArchiveOutputStream.UnicodeExtraFieldPolicy getUnicodeExtraFieldPolicy()
352+
{
353+
// Copied from ZipEncodingHelper.isUTF8()
354+
String effectiveEncoding = this.getEncoding();
355+
356+
if ( effectiveEncoding == null )
357+
{
358+
effectiveEncoding = Charset.defaultCharset().name();
359+
}
360+
361+
boolean utf8 = Charsets.UTF_8.name().equalsIgnoreCase( effectiveEncoding );
362+
363+
if ( !utf8 )
364+
{
365+
for ( String alias : Charsets.UTF_8.aliases() )
366+
{
367+
if ( alias.equalsIgnoreCase( effectiveEncoding ) )
368+
{
369+
utf8 = true;
370+
break;
371+
}
372+
}
373+
}
374+
375+
return utf8
376+
? ZipArchiveOutputStream.UnicodeExtraFieldPolicy.NOT_ENCODEABLE
377+
: ZipArchiveOutputStream.UnicodeExtraFieldPolicy.ALWAYS;
378+
379+
}
380+
342381
/**
343382
* Add the given resources.
344383
*

src/main/java/org/codehaus/plexus/archiver/zip/AbstractZipUnArchiver.java

+38-17
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,21 @@
2020
import java.io.File;
2121
import java.io.IOException;
2222
import java.io.InputStream;
23+
import java.io.UnsupportedEncodingException;
2324
import java.net.URL;
2425
import java.util.Date;
2526
import java.util.Enumeration;
27+
import javax.annotation.Nonnull;
2628

29+
import org.apache.commons.compress.archivers.zip.UnicodePathExtraField;
2730
import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
2831
import org.apache.commons.compress.archivers.zip.ZipFile;
2932
import org.apache.commons.compress.utils.IOUtils;
33+
3034
import org.codehaus.plexus.archiver.AbstractUnArchiver;
3135
import org.codehaus.plexus.archiver.ArchiverException;
3236
import org.codehaus.plexus.components.io.resources.PlexusIoResource;
3337

34-
import javax.annotation.Nonnull;
35-
3638
/**
3739
* @author <a href="mailto:[email protected]">Emmanuel Venisse</a>
3840
* @version $Id$
@@ -84,7 +86,20 @@ private static class ZipEntryFileInfo
8486

8587
public String getName()
8688
{
87-
return zipEntry.getName();
89+
try
90+
{
91+
final UnicodePathExtraField unicodePath =
92+
(UnicodePathExtraField) zipEntry.getExtraField( UnicodePathExtraField.UPATH_ID );
93+
94+
return unicodePath != null
95+
? new String( unicodePath.getUnicodeName(), "UTF-8" )
96+
: zipEntry.getName();
97+
98+
}
99+
catch ( final UnsupportedEncodingException e )
100+
{
101+
throw new AssertionError( e );
102+
}
88103
}
89104

90105
public boolean isDirectory()
@@ -141,27 +156,30 @@ protected void execute()
141156
InputStream in = null;
142157
try
143158
{
144-
zf = new org.apache.commons.compress.archivers.zip.ZipFile( getSourceFile(), encoding );
145-
final Enumeration e = zf.getEntries();
159+
zf = new org.apache.commons.compress.archivers.zip.ZipFile( getSourceFile(), encoding, true );
160+
final Enumeration e = zf.getEntriesInPhysicalOrder();
146161
while ( e.hasMoreElements() )
147162
{
148163
final ZipArchiveEntry ze = (ZipArchiveEntry) e.nextElement();
149164
final ZipEntryFileInfo fileInfo = new ZipEntryFileInfo( zf, ze );
150-
if ( isSelected( ze.getName(), fileInfo ) )
165+
if ( isSelected( fileInfo.getName(), fileInfo ) )
151166
{
152-
in = zf.getInputStream( ze );
153-
extractFileIfIncluded(getSourceFile(), getDestDirectory(), in, ze.getName(),
154-
new Date(ze.getTime()), ze.isDirectory(), ze.getUnixMode() != 0 ? ze.getUnixMode() : null,
155-
resolveSymlink( zf, ze ) );
167+
in = zf.getInputStream( ze );
168+
169+
extractFileIfIncluded( getSourceFile(), getDestDirectory(), in, fileInfo.getName(),
170+
new Date( ze.getTime() ), ze.isDirectory(),
171+
ze.getUnixMode() != 0 ? ze.getUnixMode() : null,
172+
resolveSymlink( zf, ze ) );
173+
156174
in.close();
157175
in = null;
158-
}
159-
160-
}
176+
}
177+
}
161178

162-
getLogger().debug( "expand complete" );
163179
zf.close();
164180
zf = null;
181+
182+
getLogger().debug( "expand complete" );
165183
}
166184
catch ( final IOException ioe )
167185
{
@@ -197,9 +215,9 @@ protected void execute( final String path, final File outputDirectory )
197215
InputStream in = null;
198216
try
199217
{
200-
zipFile = new org.apache.commons.compress.archivers.zip.ZipFile( getSourceFile(), encoding );
218+
zipFile = new org.apache.commons.compress.archivers.zip.ZipFile( getSourceFile(), encoding, true );
201219

202-
final Enumeration e = zipFile.getEntries();
220+
final Enumeration e = zipFile.getEntriesInPhysicalOrder();
203221

204222
while ( e.hasMoreElements() )
205223
{
@@ -213,9 +231,12 @@ protected void execute( final String path, final File outputDirectory )
213231
if ( ze.getName().startsWith( path ) )
214232
{
215233
in = zipFile.getInputStream( ze );
234+
216235
extractFileIfIncluded( getSourceFile(), outputDirectory, in,
217236
ze.getName(), new Date( ze.getTime() ), ze.isDirectory(),
218-
ze.getUnixMode() != 0 ? ze.getUnixMode() : null, resolveSymlink( zipFile, ze ) );
237+
ze.getUnixMode() != 0 ? ze.getUnixMode() : null,
238+
resolveSymlink( zipFile, ze ) );
239+
219240
in.close();
220241
in = null;
221242
}

src/test/java/org/codehaus/plexus/archiver/jar/DirectoryArchiverUnpackJarTest.java

+9-6
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import org.codehaus.plexus.PlexusTestCase;
44
import org.codehaus.plexus.archiver.Archiver;
55
import org.codehaus.plexus.archiver.util.DefaultArchivedFileSet;
6-
import org.codehaus.plexus.components.io.attributes.Java7AttributeUtils;
76
import org.codehaus.plexus.components.io.functions.InputStreamTransformer;
87
import org.codehaus.plexus.components.io.resources.PlexusIoResource;
98

@@ -12,6 +11,9 @@
1211
import java.io.IOException;
1312
import java.io.InputStream;
1413
import java.nio.charset.Charset;
14+
import org.codehaus.plexus.archiver.util.ArchiveEntryUtils;
15+
import org.codehaus.plexus.logging.Logger;
16+
import org.codehaus.plexus.logging.console.ConsoleLogger;
1517

1618
public class DirectoryArchiverUnpackJarTest
1719
extends PlexusTestCase
@@ -52,12 +54,13 @@ public void test_dependency_sets_depSet_unpacked_rdonly()
5254
archiver.createArchive();
5355
assertTrue( new File( "target/depset_unpack/child-1/META-INF/MANIFEST.MF" ).exists() );
5456

57+
final Logger logger = new ConsoleLogger( Logger.LEVEL_DEBUG, this.getClass().getName() );
5558

5659
// make them writeable or mvn clean will fail
57-
Java7AttributeUtils.chmod( new File("target/depset_unpack/child-1/META-INF"), 0777);
58-
Java7AttributeUtils.chmod( new File("target/depset_unpack/child-1/META-INF/maven"), 0777);
59-
Java7AttributeUtils.chmod( new File("target/depset_unpack/child-1/META-INF/maven/test"), 0777);
60-
Java7AttributeUtils.chmod( new File("target/depset_unpack/child-1/META-INF/maven/test/child1"), 0777);
61-
Java7AttributeUtils.chmod( new File("target/depset_unpack/child-1/assembly-resources"), 0777);
60+
ArchiveEntryUtils.chmod( new File( "target/depset_unpack/child-1/META-INF" ), 0777, logger );
61+
ArchiveEntryUtils.chmod( new File( "target/depset_unpack/child-1/META-INF/maven" ), 0777, logger );
62+
ArchiveEntryUtils.chmod( new File( "target/depset_unpack/child-1/META-INF/maven/test" ), 0777, logger );
63+
ArchiveEntryUtils.chmod( new File( "target/depset_unpack/child-1/META-INF/maven/test/child1" ), 0777, logger );
64+
ArchiveEntryUtils.chmod( new File( "target/depset_unpack/child-1/assembly-resources" ), 0777, logger );
6265
}
6366
}

0 commit comments

Comments
 (0)