Zip

(System Library)

Description: Generates a zip archive of a file stream.
Returns:

Error on failure / Invalid on successful completion

(Zip file returned via the second parameter)

Usage: Script only.
Function Groups: Stream and Buffer
Related to:

 

Format: \System.Zip(FileDictionary, OutputStream)
Parameters:  
FileDictionary

Required Dictionary. This contains a key and one value:

Key: The relative file path. May be a single value or an array of file paths.

Value: An associated FileData structure for each key.

  FileData Struct [
    FileStreamData    { File Stream };
  ];.
OutputStream
Required. A stream that will contain a zipped version of the specified file(s).
Comments:

This module is a member of the System Library, and must therefore be prefaced by \System. as shown in the "Format" section.

If your application predates version 11.2, use the backslash notation rather than dot: \System\

Work is done in-memory. This function can take either a file or a BuffStream as input. The output is generated in memory and must be written to storage by your code.

Example:

The following creates three zip files, illustrating three ways to use the function.

[
  FStream;
  FStream2;
  FStream3;
  FileData Struct [
    FileStreamData;
  ];
  FileDic                           { Dictionary to test with                 };
  FileDic2;
  FileDic3;
  MyZipFile                         { Output zip streams                      };
  MyZipFile2;
  MyZipFile3;
  Constant 1MiB = 1024 * 1024;
  temp;
]

Main [
  If Watch(1);
  [
    MyZipFile = BuffStream("");
    MyZipFile2 = BuffStream("");
    MyZipFile3 = BuffStream("");

     { Test a simple two-file zip }
    FileDic = Dictionary();
    FileDic["File1.txt"] = FileData(BuffStream("This is File1"));
    FileDic["File2.txt"] = FileData(BuffStream("This is File2"));
    \System.Zip(FileDic, MyZipFile);

     { Test nested directories }
    FileDic2 = Dictionary();
    FileDic2["Folder1/File1.txt"]                 = FileData(BuffStream("This is Folder1/File1"));
    FileDic2["Folder1/Folder2/File2.txt"]         = FileData(BuffStream("This is Folder1/Folder2/File2"));
    FileDic2["Folder1/Folder2/Folder3/File3.txt"] = FileData(BuffStream("This is Folder1/Folder2/Folder3/File3"));
    \System.Zip(FileDic2, MyZipFile2);

     { Large file test }
    FileDic3 = Dictionary();
    FileDic3["File1.txt"] = FileData(BuffStream(MakeBuff(1MiB*1024, 0x41)));
    \System.Zip(FileDic3, MyZipFile3);

    FStream = FileStream("C:\Path\To\MyZip\MyZip.zip");
    FStream2 = FileStream("C:\Path\To\MyZip\MyZip2.xlsx");
    FStream3 = FileStream("C:\Path\To\MyZip\MyZip3.zip");
    BlockWrite(FStream, MyZipFile);
    BlockWrite(FStream2, MyZipFile2);
    BlockWrite(FStream3, MyZipFile3);
    Return();
  ]
]