std.zip for Binary example

locco via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sat Jan 23 06:58:23 PST 2016


On Sunday, 17 January 2016 at 10:43:55 UTC, Sean Campbell wrote:
> On Sunday, 17 January 2016 at 10:34:19 UTC, locco wrote:
>> Hi :)
>> I found this example:
>> ======================================================================
>> import std.file: write;
>> import std.string: representation;
>> void main()
>> {
>>    char[] data = "Test data.\n".dup;
>>    // Create an ArchiveMember for the test file.
>>    ArchiveMember am = new ArchiveMember();
>>    am.name = "test.txt";
>>    am.expandedData(data.representation);
>>    // Create an archive and add the member.
>>    ZipArchive zip = new ZipArchive();
>>    zip.addMember(am);
>>    // Build the archive
>>    void[] compressed_data = zip.build();
>>    // Write to a file
>>    write("test.zip", compressed_data);
>> }
>> ======================================================================
>> But i cound't find example code for binary file.
>> How can i make ArciveMember for binary file?
>
>
> std.zip dosen't discriminate against text and binary files 
> files.
> p.s. remember to import std.zip

================================================================================
import std.stdio;
import std.file:write,getSize;
import std.string: representation;
import std.zip: ArchiveMember, ZipArchive;

void main() {
     auto stream = File("example.jpg", "r+");

     auto inbytes = new ubyte[ cast(uint)getSize("example.jpg") ];
     stream.rawRead(inbytes);

     auto zip = new ZipArchive();
     ArchiveMember ae = new ArchiveMember();
     ae.name = "example_.jpg";
     ae.expandedData( inbytes );

     zip.addMember(ae);
     void[] compressed_data = zip.build();
     write("test.zip", compressed_data);
}
================================================================================

I made this, work fine. But i guess it wasn't nice solution. 
Right?


More information about the Digitalmars-d-learn mailing list