Skip to content

Commit b27fa30

Browse files
author
trygvis
committed
Solving parts of PLX-148.
Applying patches from Edwin Punzalan and Daniel Krisher. o Adding a field to optionally remove empty directories. This used to be the default.
1 parent fa36487 commit b27fa30

File tree

3 files changed

+135
-3
lines changed

3 files changed

+135
-3
lines changed

src/main/java/org/codehaus/plexus/archiver/AbstractArchiver.java

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,12 @@ public abstract class AbstractArchiver extends AbstractLogEnabled
4444
private File destFile;
4545

4646
private Map filesMap = new HashMap();
47+
48+
private Map dirsMap = new HashMap();
4749

4850
private String prefix;
51+
52+
private boolean includeEmptyDirs = true;
4953

5054
public void addDirectory( File directory )
5155
throws ArchiverException
@@ -64,7 +68,7 @@ public void addDirectory( File directory, String[] includes, String[] excludes )
6468
{
6569
addDirectory( directory, "", includes, excludes );
6670
}
67-
71+
6872
public void addDirectory( File directory, String prefix, String[] includes, String[] excludes )
6973
throws ArchiverException
7074
{
@@ -133,6 +137,23 @@ public void addDirectory( File directory, String prefix, String[] includes, Stri
133137
filesMap.put( file, new File( basedir, file ) );
134138
}
135139
}
140+
141+
if ( includeEmptyDirs )
142+
{
143+
String[] dirs = scanner.getIncludedDirectories();
144+
for ( int i = 0; i < dirs.length; i++ ) {
145+
String dir = dirs[i];
146+
dir = dir.replace( '\\', '/' );
147+
if ( prefix != null )
148+
{
149+
getDirs().put( prefix + dir, new File( basedir, dir ) );
150+
}
151+
else
152+
{
153+
getDirs().put( dir, new File( basedir, dir ) );
154+
}
155+
}
156+
}
136157
}
137158

138159
public void addFile( File inputFile, String destFileName )
@@ -149,7 +170,15 @@ public void addFile( File inputFile, String destFileName )
149170

150171
protected Map getFiles()
151172
{
152-
return filesMap;
173+
if ( !includeEmptyDirs ) return filesMap;
174+
175+
Map resources = new HashMap();
176+
177+
resources.putAll( filesMap );
178+
179+
resources.putAll( getDirs() );
180+
181+
return resources;
153182
}
154183

155184
public File getDestFile()
@@ -179,4 +208,19 @@ protected Logger getLogger()
179208

180209
return logger;
181210
}
211+
212+
public boolean getIncludeEmptyDirs()
213+
{
214+
return includeEmptyDirs;
215+
}
216+
217+
public void setIncludeEmptyDirs(boolean includeEmptyDirs)
218+
{
219+
this.includeEmptyDirs = includeEmptyDirs;
220+
}
221+
222+
public Map getDirs()
223+
{
224+
return dirsMap;
225+
}
182226
}

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -484,7 +484,14 @@ protected final void addResources( Map resources, ZipOutputStream zOut )
484484

485485
addParentDirs(base, name, zOut, "", dirMode);
486486

487-
zipFile(resource, zOut, name, fileMode);
487+
if ( resource.isFile() )
488+
{
489+
zipFile(resource, zOut, name, fileMode);
490+
}
491+
else
492+
{
493+
zipDir( resource, zOut, name, dirMode );
494+
}
488495
}
489496
}
490497

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
package org.codehaus.plexus.archiver;
2+
3+
/*
4+
* The MIT License
5+
*
6+
* Copyright (c) 2004, The Codehaus
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy of
9+
* this software and associated documentation files (the "Software"), to deal in
10+
* the Software without restriction, including without limitation the rights to
11+
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
12+
* of the Software, and to permit persons to whom the Software is furnished to do
13+
* so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in all
16+
* copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24+
* SOFTWARE.
25+
*/
26+
27+
import org.codehaus.plexus.PlexusTestCase;
28+
import org.codehaus.plexus.archiver.zip.ZipArchiver;
29+
import org.codehaus.plexus.archiver.zip.ZipUnArchiver;
30+
31+
import java.io.File;
32+
33+
/**
34+
* @author Daniel Krisher
35+
* @version $Id$
36+
*/
37+
public class EmptyDirectoryTest
38+
extends PlexusTestCase
39+
{
40+
public void testEmptyDirectory()
41+
throws Exception
42+
{
43+
ZipArchiver archiver = (ZipArchiver) lookup( Archiver.ROLE, "zip" );
44+
45+
//Should default to true...
46+
assertTrue( archiver.getIncludeEmptyDirs() );
47+
48+
//create an empty directory to store in the zip archive
49+
File emptyDir = getTestFile( "target/output/emptyTest/TmpEmptyDir" );
50+
//delete it if it exists to ensure it is actually empty
51+
if ( emptyDir.exists() )
52+
{
53+
emptyDir.delete();
54+
}
55+
emptyDir.mkdirs();
56+
archiver.addDirectory( emptyDir.getParentFile() );
57+
58+
File archive = getTestFile( "target/output/emptyDirArchive.zip" );
59+
if ( archive.exists() )
60+
{
61+
archive.delete();
62+
}
63+
64+
archiver.setDestFile( archive );
65+
archiver.createArchive();
66+
67+
//delete the empty dir, we will extract it from the archive
68+
emptyDir.delete();
69+
70+
//Check the content of the archive by extracting it
71+
72+
ZipUnArchiver unArchiver = (ZipUnArchiver) lookup( UnArchiver.ROLE, "zip" );
73+
unArchiver.setSourceFile( archive );
74+
75+
unArchiver.setDestDirectory( getTestFile( "target/output/emptyTest" ) );
76+
unArchiver.extract();
77+
78+
assertTrue( emptyDir.exists() );
79+
assertTrue( emptyDir.isDirectory() );
80+
}
81+
}

0 commit comments

Comments
 (0)