Skip to content

Commit 122455e

Browse files
committed
Added a separate class and extracted some logic
1 parent efc25af commit 122455e

File tree

3 files changed

+87
-34
lines changed

3 files changed

+87
-34
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -391,7 +391,7 @@ private void createIndexList( ParallelScatterZipCreator zOut )
391391

392392
// filter out META-INF if it doesn't contain anything other than the index and manifest.
393393
// this is what sun.misc.JarIndex does, guess we ought to be consistent.
394-
Set<String> filteredDirs = new HashSet<String>( addedDirs.keySet() );
394+
Set<String> filteredDirs = addedDirs.allAddedDirs();
395395
// our added dirs always have a trailing slash
396396
if ( filteredDirs.contains( META_INF_NAME + '/' ) )
397397
{

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

Lines changed: 8 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -79,16 +79,11 @@ public abstract class AbstractZipArchiver
7979

8080
protected String archiveType = "zip";
8181

82-
/*
83-
* Whether the original compression of entries coming from a ZIP
84-
* archive should be kept (for example when updating an archive).
85-
*/
86-
//not used: private boolean keepCompression = false;
8782
private boolean doFilesonly = false;
8883

8984
protected final Hashtable<String, String> entries = new Hashtable<String, String>();
9085

91-
protected final Hashtable<String, String> addedDirs = new Hashtable<String, String>();
86+
protected final AddedDirs addedDirs = new AddedDirs();
9287

9388
private static final long EMPTY_CRC = new CRC32().getValue();
9489

@@ -363,7 +358,7 @@ protected final void addResources( ResourceIterator resources, ParallelScatterZi
363358
name = name + "/";
364359
}
365360

366-
addParentDirs( entry, null, name, zOut, "" );
361+
addParentDirs( entry, null, name, zOut);
367362

368363
if ( entry.getResource().isFile() )
369364
{
@@ -384,29 +379,12 @@ protected final void addResources( ResourceIterator resources, ParallelScatterZi
384379
* be impossible and is not really supported.
385380
*/
386381
@SuppressWarnings( { "JavaDoc" } )
387-
private void addParentDirs( ArchiveEntry archiveEntry, File baseDir, String entry, ParallelScatterZipCreator zOut,
388-
String prefix )
382+
private void addParentDirs(ArchiveEntry archiveEntry, File baseDir, String entry, ParallelScatterZipCreator zOut)
389383
throws IOException
390384
{
391385
if ( !doFilesonly && getIncludeEmptyDirs() )
392386
{
393-
Stack<String> directories = new Stack<String>();
394-
395-
// Don't include the last entry itself if it's
396-
// a dir; it will be added on its own.
397-
int slashPos = entry.length() - ( entry.endsWith( "/" ) ? 1 : 0 );
398-
399-
while ( ( slashPos = entry.lastIndexOf( '/', slashPos - 1 ) ) != -1 )
400-
{
401-
String dir = entry.substring( 0, slashPos + 1 );
402-
403-
if ( addedDirs.contains( prefix + dir ) )
404-
{
405-
break;
406-
}
407-
408-
directories.push( dir );
409-
}
387+
Stack<String> directories = addedDirs.asStringStack(entry);
410388

411389
while ( !directories.isEmpty() )
412390
{
@@ -423,12 +401,12 @@ private void addParentDirs( ArchiveEntry archiveEntry, File baseDir, String entr
423401
// the
424402
// At this point we could do something like read the atr
425403
final PlexusIoResource res = new AnonymousResource( f );
426-
zipDir( res, zOut, prefix + dir, archiveEntry.getDefaultDirMode(), encoding );
404+
zipDir( res, zOut, dir, archiveEntry.getDefaultDirMode(), encoding );
427405
}
428406
}
429407
}
430408

431-
/**
409+
/**
432410
* Adds a new entry to the archive, takes care of duplicates as well.
433411
*
434412
* @param in the stream to read data for the entry from.
@@ -544,15 +522,12 @@ protected void zipDir( PlexusIoResource dir, ParallelScatterZipCreator zOut, Str
544522
String encodingToUse )
545523
throws IOException
546524
{
547-
if ( addedDirs.get( vPath ) != null )
525+
if ( addedDirs.update(vPath) )
548526
{
549-
// don't add directories we've already added.
550-
// no warning if we try, it is harmless in and of itself
551527
return;
552528
}
553529

554-
getLogger().debug( "adding directory " + vPath );
555-
addedDirs.put( vPath, vPath );
530+
getLogger().debug("adding directory " + vPath);
556531

557532
if ( !skipWriting )
558533
{
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package org.codehaus.plexus.archiver.zip;
2+
3+
/**
4+
*
5+
* Copyright 2015 The Apache Software Foundation
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
*/
19+
20+
21+
import java.util.HashSet;
22+
import java.util.Hashtable;
23+
import java.util.Set;
24+
import java.util.Stack;
25+
26+
/**
27+
* A list of directories that have been added to an archive.
28+
*/
29+
public class AddedDirs {
30+
31+
private final Hashtable<String, String> addedDirs = new Hashtable<String, String>();
32+
33+
public Stack<String> asStringStack(String entry) {
34+
Stack<String> directories = new Stack<String>();
35+
36+
// Don't include the last entry itself if it's
37+
// a dir; it will be added on its own.
38+
int slashPos = entry.length() - ( entry.endsWith( "/" ) ? 1 : 0 );
39+
40+
while ( ( slashPos = entry.lastIndexOf( '/', slashPos - 1 ) ) != -1 )
41+
{
42+
String dir = entry.substring( 0, slashPos + 1 );
43+
44+
if ( addedDirs.contains( dir ) )
45+
{
46+
break;
47+
}
48+
49+
directories.push( dir );
50+
}
51+
return directories;
52+
}
53+
54+
public void clear() {
55+
addedDirs.clear();
56+
}
57+
58+
59+
/**
60+
* Adds the path to this list.
61+
* @param vPath The path to add.
62+
* @return true if the path was not present, false if it already existed.
63+
*/
64+
public boolean update(String vPath){
65+
if ( addedDirs.get( vPath ) != null )
66+
{
67+
// don't add directories we've already added.
68+
// no warning if we try, it is harmless in and of itself
69+
return true;
70+
}
71+
addedDirs.put( vPath, vPath );
72+
return false;
73+
}
74+
75+
public Set<String> allAddedDirs(){
76+
return new HashSet<String>( addedDirs.keySet() );
77+
}
78+
}

0 commit comments

Comments
 (0)