<commit_before>--[[ compress.lua
    This example shows how a the compress module can be used to create zip files and write them to
    a compatible ostream.  Files and directories can be added via istreams and filesystem paths.
--]]

-- the ostream 
local fostream = assert(require("poco.fileostream"))
local compress = assert(require("poco.zip.compress"))
local path = assert(require("poco.path"))

-- write the zip file to the output stream in the temp directory.
local output_zip_path = path(path.temp())
output_zip_path:setFileName("compress_example.zip")
local zfs = assert(fostream(output_zip_path:toString()))

-- create compress instance with the fileostream.
--- compress stores a reference to the fileostream to prevent GC, 
--- so a reference does not need to be maintained by the caller.
--- additionally, the default true parameter for seekable must be false for 
--- non-seekable istreams such as pipeistream, socketistream, etc.
local c = assert(compress(zfs))

-- print the list of file extensions that will get stored in the zipfile
-- via the STORE method rather than DEFLATE.
print("\nFile extensions using STORE method: ")
for k,v in pairs(c:getStoreExtensions()) do print(k,v) end

-- store a comment for the zip file and print it out.
c:setZipComment("example comment")
print("\nZip file comment:", c:getZipComment())

-- assume current working directory is the directory containing the examples.
local examples_path = assert(path(path.current()))
-- add all files and directories under the 'examples' directory to the zip file.
io.write(string.format("\nAdding recursive directory: %s\nto: %s\n", examples_path:toString(), output_zip_path:toString()))
assert(c:addRecursive(examples_path, "examples"))

-- add README.md to readme/README.md via the addFile function.
examples_path:popDirectory()
examples_path:setFileName("README.md")
io.write(string.format("\nAdding File: %s\nto: %s\n", examples_path:toString(), output_zip_path:toString()))
-- assert(c:addFile(examples_path, "readme/README.md"))

--[[ alternatively one could add a file via an poco istream via the addIStream function.
-- note that this source could be a memoryistream, pipeistream, socketistream, etc.
local fistream = assert(require("poco.fileistream"))
local fis = assert(fistream(examples_path:toString()))
--]]

-- finalize the zip file.
c:close()<commit_msg>uncomment addFile() example, fix ostream comment<commit_after>--[[ compress.lua
    This example shows how a the compress module can be used to create zip files and write them to
    a compatible ostream.  Files and directories can be added via istreams and filesystem paths.
--]]

-- the ostream could be other types, such as pipeostream, teeostream, memoryostream, etc.
local fostream = assert(require("poco.fileostream"))
local compress = assert(require("poco.zip.compress"))
local path = assert(require("poco.path"))

-- write the zip file to the output stream in the temp directory.
local output_zip_path = path(path.temp())
output_zip_path:setFileName("compress_example.zip")
local zfs = assert(fostream(output_zip_path:toString()))

-- create compress instance with the fileostream.
--- compress stores a reference to the fileostream to prevent GC, 
--- so a reference does not need to be maintained by the caller.
--- additionally, the default true parameter for seekable must be false for 
--- non-seekable istreams such as pipeistream, socketistream, etc.
local c = assert(compress(zfs))

-- print the list of file extensions that will get stored in the zipfile
-- via the STORE method rather than DEFLATE.
print("\nFile extensions using STORE method: ")
for k,v in pairs(c:getStoreExtensions()) do print(k,v) end

-- store a comment for the zip file and print it out.
c:setZipComment("example comment")
print("\nZip file comment:", c:getZipComment())

-- assume current working directory is the directory containing the examples.
local examples_path = assert(path(path.current()))
-- add all files and directories under the 'examples' directory to the zip file.
io.write(string.format("\nAdding recursive directory: %s\nto: %s\n", examples_path:toString(), output_zip_path:toString()))
assert(c:addRecursive(examples_path, "examples"))

-- add README.md to readme/README.md via the addFile function.
examples_path:popDirectory()
examples_path:setFileName("README.md")
io.write(string.format("\nAdding File: %s\nto: %s\n", examples_path:toString(), output_zip_path:toString()))
assert(c:addFile(examples_path, "readme/README.md"))

--[[ alternatively one could add a file via an poco istream via the addIStream function.
-- note that this source could be a memoryistream, pipeistream, socketistream, etc.
local fistream = assert(require("poco.fileistream"))
local fis = assert(fistream(examples_path:toString()))
--]]

-- finalize the zip file.
c:close()
{'fraction_non_alphanumeric': 0.11032766486934882, 'fraction_numerical': 0.0, 'mean_word_length': 5.1283354510800505, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6113474', 'n_tokens_mistral': 1394, 'n_tokens_neox': 1354, 'n_words': 518}