You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@ant.apache.org by "Arne v.Irmer" <Ar...@tu-dortmund.de> on 2009/09/22 15:50:51 UTC

Folder-Comments

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

Re: Folder-Comments

Posted by Andy Stevens <in...@googlemail.com>.
2009/9/22 Arne v.Irmer <Ar...@tu-dortmund.de>:
> 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");

In the souce code for ZipEntry I see
     public boolean isDirectory() {
         return getName().endsWith("/");
     }
so I expect that should be new ZipEntry("TestFolder/")...
"TestFolder" is a perfectly valid name for a file - extensions aren't
mandatory, especially on non-windows systems.  The fact you don't call
out.write() for folderEntry explains why it then has a length of 0,
but asides from that it looks to me that you're creating two files,
one of which happens to be in a folder with the same name as the first
one (hence the errors you get when unzipping).

I'm curious what exactly this has to do with Ant, though?


Andy
-- 
http://pseudoq.sourceforge.net/  open source java sudoku application

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
For additional commands, e-mail: user-help@ant.apache.org


Re: Folder-Comments

Posted by "Arne v.Irmer" <Ar...@tu-dortmund.de>.
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