Is there a std.zip.ZipArchive isDir or isFile method?

cc cc at nevernet.com
Wed Feb 12 05:59:53 UTC 2020


On Monday, 3 February 2020 at 13:26:38 UTC, mark wrote:
> I'm using std.zip.ZipArchive to read zip files, e.g.:
>
>     auto zip = new ZipArchive(read(filename));
>     // ...
>     foreach (name, member; zip.directory) {
> 	if (name.endsWith('/')) // skip dirs
> 	    continue;
> 	mkdirRecurse(dirName(name));
> 	zip.expand(member);
> 	write(name, member.expandedData());
>     }
>
> As you can see, I am detecting directories with a crude test.
>
> I really wish there was a method for this: and if there is, 
> could you give me the link 'cos I can't see one in the docs?
>
> (BTW The code above is slightly simplified: the real code won't 
> unzip if there's an absolute path or .. present and also 
> ensures that all members are unzipped into a subdir even if the 
> zip has top-level names.)

I couldn't find one either, I had to do this:

version(Windows) {
	enum uint FILE_ATTRIBUTE_DIRECTORY = 0x10;
}
auto zip = new ZipArchive(buffer);
foreach (fn, am; zip.directory) {
	if (am.fileAttributes & FILE_ATTRIBUTE_DIRECTORY)
		... is directory
	else
		... is file
}

As I'm looking at my code for this I'm also reminded that 
different zip files can internally store path separators as 
either \ or / depending on the platform that created them so you 
may need to be careful about that too.  I have a bit for this 
that simply does:
version(StandardizePathSeparators) {
	string filename = fn.replace("\\", "/");
} else {
	string filename = fn;
}




More information about the Digitalmars-d-learn mailing list