cr_zip_tricks~mamantoha
cr_zip_tricks
An alternate ZIP writer for Crystal, ported from zip_tricks for Ruby
Installation
- Add the dependency to your
shard.yml
:
dependencies:
cr_zip_tricks:
github: WeTransfer/cr_zip_tricks
- Run
shards install
Usage
Archiving to any IO:
require "zip_tricks"
ZipTricks::Streamer.archive(STDOUT) do |s|
s.add_deflated("deflated.txt") do |sink|
sink << "Hello stranger! This is a chunk of text that is going to compress. Well."
end
s.add_stored("stored.txt") do |sink|
sink << "Goodbye stranger!"
end
end
Sizing an archive before creation, to the byte:
require "cr_zip_tricks"
size = ZipTricks::Sizer.size do |s|
s.predeclare_entry(filename: "deflated1.txt", uncompressed_size: 8969887, compressed_size: 1245, use_data_descriptor: true)
s.predeclare_entry(filename: "deflated2.txt", uncompressed_size: 4568, compressed_size: 4065, use_data_descriptor: true)
end
size #=> 5641
Using it with Kemal or other web app skeleton
Here is a Kemal app that outputs itself, 1000 times, compressed:
require "kemal"
require "cr_zip_tricks"
get "/quine.zip" do |env|
env.response.headers["Content-Type"] = "application/octet-stream"
env.response.headers["Content-Disposition"] = "attachment"
ZipTricks::Streamer.archive(env.response) do |s|
1000.times do |i|
s.add_deflated("cr_download_server_%05d.cr" % i) do |sink|
File.open(__FILE__, "rb") do |f|
IO.copy(f, sink)
end
end
end
end
end
Kemal.run
Development
TODO: Write development instructions here
Contributing
- Fork it (https://github.com/WeTransfer/cr_zip_tricks/fork)
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create a new Pull Request
Contributors
- Julik Tarkhanov - creator and maintainer