ant-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Arne v.Irmer" <Arne.vonIr...@tu-dortmund.de>
Subject Re: Folder-Comments
Date Thu, 24 Sep 2009 08:44:49 GMT
Hi,
analysing the code solves the problem.
First all folders that will be added to a zip must end with a slash!
Second a ZipEntry must be marked explicit as a folder by setting the 4th bit of the ExternalAttributes.

This will be done by setting the UnixMode of a ZipEntry. 
Here a complete example:

package org.example;

import java.io.BufferedOutputStream;
import java.io.FileOutputStream;

import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipOutputStream;

public class ZipTest {

	public final static long ZIPENTRY_IS_DIRECTORY = 1<<4;
	public final static String WINDOWS_ZIP_ENCODING="Cp437";
	public final static String NON_WINDOWS_ZIP_ENCODING="UTF-8";
	
	public static void main (String argv[]) {
		try {
            // Bitmask indicating directories in 'external attributes' of a ZIP archive entry.

			FileOutputStream dest = new FileOutputStream("/tmp/checkFolder.zip");
			ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(dest));
			out.setEncoding(WINDOWS_ZIP_ENCODING);
			
			ZipEntry folderEntry = new ZipEntry("TestFolder/");
			folderEntry.setExternalAttributes(folderEntry.getExternalAttributes()|ZIPENTRY_IS_DIRECTORY);
			folderEntry.setComment("My Folder comment.");
			out.putNextEntry(folderEntry);
			out.closeEntry();
			
			ZipEntry fileEntry = new ZipEntry("TestFolder/TestFile.txt");
			fileEntry.setComment("My File comment.");
			fileEntry.setUnixMode(0666);
			out.putNextEntry(fileEntry);
			out.write("Hallo Zip!".getBytes());
			out.closeEntry();
			
			out.close();
		} catch(Exception e) {
			e.printStackTrace();
		}
	}
}

This example should build zip-Files that are compatible with most platforms.

Yours 
 Arne 

> Hi,
> with the command line program "zip" I can create a zip-File with annotated folders inside.
I try to generate such a zip-File with the org.apache.tools.zip classes coming with ant.jar.

> 
> Here is my small example program:
> 
> package org.example.zip;
> 
> import java.io.BufferedOutputStream;
> import java.io.FileOutputStream;
> 
> import org.apache.tools.zip.ZipEntry;
> import org.apache.tools.zip.ZipOutputStream;
> 
> public class ZipTest {
> 	public static void main (String argv[]) {
> 		try {
> 			FileOutputStream dest = new FileOutputStream("/tmp/checkFolder.zip");
> 			ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(dest));
> 
> 			ZipEntry folderEntry = new ZipEntry("TestFolder");
> 			folderEntry.setComment("My Folder comment.");
> 			out.putNextEntry(folderEntry);
> 			out.closeEntry();
> 			
> 			ZipEntry fileEntry = new ZipEntry("TestFolder/TestFile.txt");
> 			fileEntry.setComment("My File comment.");
> 			out.putNextEntry(fileEntry);
> 			out.write("Hallo Zip!".getBytes());
> 			out.closeEntry();
> 			
> 			out.close();
> 		} catch(Exception e) {
> 			e.printStackTrace();
> 		}
> 	}
> }
> 
> On a first look with unzip everything looks nice:
> Archive:  /tmp/checkFolder.zip
>   Length     Date   Time    Name
>  --------    ----   ----    ----
>         0  09-22-09 15:09   TestFolder
> My Folder comment.
>        10  09-22-09 15:09   TestFolder/TestFile.txt
> My File comment.
>  --------                   -------
>        10                   2 files
> 
> But the build in windows Zip always finds an empty file with the name of the folder and
unzip has a problem to extract it:
> unzip  /tmp/checkFolder.zip                                                       
> Archive:  /tmp/checkFolder.zip                                                      
                
>   inflating: TestFolder                                                             
                
> checkdir error:  TestFolder exists but is not directory                             
                
>                  unable to process TestFolder/TestFile.txt.     
> 
> Is there a way to tell a ZipEntry that it is folder?
> How can I write a commented folder to a zip file?
> 
> Yours
>  Arne
> 

-- 
Dipl.-Phys. Arne v.Irmer
Senior Developer
Anwendungsentwicklung - ITMC
Technische Universit├Ąt Dortmund
GB V: Raum 301
August Schmidt Str.12
44227 Dortmund
Tel.: ++49 231 755 7127
Fax : ++49 231 755 2731

Mime
View raw message