www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Directories in zip

reply "nobody" <somebody somewhere.com> writes:
(I'm using D1)
Is it possible to create directories in a zip with std.zip? I couldn't find 
any info about that.
So far I've only figured out how to read/write files. (code below)

Or should I be using something else entirely if I want to create archives?

---
module main;

import std.stdio: writefln;
import std.file: read, write;
import std.zip;

void main()
{
 auto archive = new ZipArchive;
 auto member = new ArchiveMember;

 member.compressionMethod = 8; //0: none, 8: deflate
 member.name = `hello.txt`;
 member.expandedData = cast(ubyte[])`Hello`;
 archive.addMember(member);

 member = new ArchiveMember;
 member.compressionMethod = 8;
 member.name = `world.txt`;
 member.expandedData = cast(ubyte[])`World!`;
 archive.addMember(member);

 auto data = archive.build();
 write(`test.zip`,data);
 auto text = read(`test.zip`);
 auto archive2 = new ZipArchive(text);

 foreach (ArchiveMember mem; archive2.directory)
 {
  archive2.expand(mem);
  writefln(cast(char[])mem.expandedData);
 }
}
--- 
Dec 18 2009
parent "nobody" <somebody somewhere.com> writes:
Ah, well, it seems simply putting the directory in the member.name (ie. 
"dir\test.txt") is the way to do it.

Move along, nothing to see here.

"nobody" <somebody somewhere.com> wrote in message 
news:hggvh2$1t67$1 digitalmars.com...
 (I'm using D1)
 Is it possible to create directories in a zip with std.zip? I couldn't 
 find any info about that.
 So far I've only figured out how to read/write files. (code below)

 Or should I be using something else entirely if I want to create archives?

 ---
 module main;

 import std.stdio: writefln;
 import std.file: read, write;
 import std.zip;

 void main()
 {
 auto archive = new ZipArchive;
 auto member = new ArchiveMember;

 member.compressionMethod = 8; //0: none, 8: deflate
 member.name = `hello.txt`;
 member.expandedData = cast(ubyte[])`Hello`;
 archive.addMember(member);

 member = new ArchiveMember;
 member.compressionMethod = 8;
 member.name = `world.txt`;
 member.expandedData = cast(ubyte[])`World!`;
 archive.addMember(member);

 auto data = archive.build();
 write(`test.zip`,data);
 auto text = read(`test.zip`);
 auto archive2 = new ZipArchive(text);

 foreach (ArchiveMember mem; archive2.directory)
 {
  archive2.expand(mem);
  writefln(cast(char[])mem.expandedData);
 }
 }
 --- 

 
Dec 22 2009