Bug 12267 - Add ability to unzip into separate folders
Summary: Add ability to unzip into separate folders
Status: NEW
Alias: None
Product: Ant
Classification: Unclassified
Component: Core tasks (show other bugs)
Version: 1.5
Hardware: All All
: P3 enhancement (vote)
Target Milestone: ---
Assignee: Ant Notifications List
URL:
Keywords: PatchAvailable
Depends on:
Blocks:
 
Reported: 2002-09-03 16:37 UTC by Adam Mlodzinski
Modified: 2009-07-30 07:18 UTC (History)
0 users



Attachments
Patch to add mapper capability to Expand.java (unjar/tar/war/zip tasks). (2.83 KB, patch)
2003-05-18 01:32 UTC, Mark Crocker
Details | Diff
TestCase patch for mapper capability in Expand.java (1.55 KB, patch)
2003-05-18 01:34 UTC, Mark Crocker
Details | Diff
Supporting and build file for TestCase (1.07 KB, patch)
2003-05-18 01:35 UTC, Mark Crocker
Details | Diff
Patch for unzip documentation changes related to Expand.java mapper addition. (4.30 KB, patch)
2003-05-18 01:37 UTC, Mark Crocker
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Adam Mlodzinski 2002-09-03 16:37:57 UTC
This is a request for the ant unzip task to be able to unzip a <fileset> of zip
files to newly created folders based on the zip file name. In other words,
giving the unzip task a number of zip files, rather than expand them into the
folder specified by the dest attribute, use the base name of the zip file as the
name of a new subfolder created in dest, then expand the contents of the zip
file into that subfolder.
For example, the following unzip task
<unzip dest="extracts" separate="true">
	<fileset dir="${zipFolder}" >
	  <include name="*.zip" >
	</fileset>
</unzip>, 
where zipFolder contains buildA.zip and buildB.zip, would expand buildA.zip to
extracts/buildA and buildB.zip to extracts/buildB.
The following additional code inserted at the beginning of 
org.apache.tools.ant.taskdefs.Expand.expandFile(FileUtils, File, File)
implements this ability:
protected void expandFile(FileUtils fileUtils, File srcF, File destDir) {
    File dir = destDir;
    if (separate) {
        String fileNameBase = srcF.getName();
        fileNameBase = fileNameBase.substring(0, fileNameBase.indexOf('.'));
        dir = new File(destDir,fileNameBase);
        dir.mkdir();
    }
    log("Expanding: " + srcF + " into " + dir, Project.MSG_INFO);
    ZipInputStream zis = null;
    [continues unchanged]

I don't think this accounts for zip files in subdirectories - which would
probably need to have the relative path recreated to avoid filename conflicts. 

Mundane info:
separate is defined as
    private boolean separate = false;  // false default maintains previous
behaviour when not used.

and the attribute is made available by
    /**
     * Should files in filesets or src be extracted to new subdirectories
     * of the same name?
     */
    public void setSeparate(boolean b){
        separate = b;
    }
Comment 1 Mark Crocker 2003-05-16 17:00:38 UTC
I am working on a different solution to the same problem.  I believe that my solution is more 
elegant because it simply adds a Mapper to the unjar, untar, unwar unzip tasks.  This is much 
more flexible and consistent with the copy task.  

Should I submit my code to this bug or create a new one?

An example of my proposed use is:

	<unjar dest="${dir.preverified}">
	  <fileset dir="${dir.src}">
	    <include name="*.jar" />
	    <include name="*.zip" />
	  </fileset>
	  <mapper type="regexp" from="^(.*)\.(jar|zip)$$" to="\1/" />
	</unjar>

	This unpacks all files ending in .jar or .zip in the source directory and puts the contents of 
each into a subdirectory of the preverified directory with the same name as the original archive file 
without the trailing .jar or .zip. For example, the contents of src/purifier.zip and src/ant.jar will be 
extracted to preverified/purifier/ and preverified/ant/ directories respectively.
Comment 2 Antoine Levy-Lambert 2003-05-16 20:27:18 UTC
It is perfectly OK for you to submit your code to this bug.
Comment 3 Mark Crocker 2003-05-18 01:32:55 UTC
Created attachment 6401 [details]
Patch to add mapper capability to Expand.java (unjar/tar/war/zip tasks).
Comment 4 Mark Crocker 2003-05-18 01:34:52 UTC
Created attachment 6402 [details]
TestCase patch for mapper capability in Expand.java
Comment 5 Mark Crocker 2003-05-18 01:35:51 UTC
Created attachment 6403 [details]
Supporting and build file for TestCase
Comment 6 Mark Crocker 2003-05-18 01:37:10 UTC
Created attachment 6404 [details]
Patch for unzip documentation changes related to Expand.java mapper addition.
Comment 7 Adam Mlodzinski 2003-10-03 18:56:45 UTC
I think I should have been more specific in this request. I need the ability to
selectively extract into a single directory or into separate directories based
on the archive filename.
While I agree that the <mapper/> capability is more elegant, I don't see any way
to apply it selectively, other than setting/not setting a new property, and
calling two different targets that use if or unless, or using ant-contrib's
if/else. I think either of these methods counter the elegance provided by the
mapper solution. But that's just me.
Maybe the ideal solution would be to add if/unless capability to the (built-in)
<mapper/>'s. Comments?