Skip to content

Commit

Permalink
First Release
Browse files Browse the repository at this point in the history
  • Loading branch information
naqvis committed Sep 27, 2019
0 parents commit 2e54dc0
Show file tree
Hide file tree
Showing 12 changed files with 880 additions and 0 deletions.
9 changes: 9 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
root = true

[*.cr]
charset = utf-8
end_of_line = lf
insert_final_newline = true
indent_style = space
indent_size = 2
trim_trailing_whitespace = true
9 changes: 9 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/docs/
/lib/
/bin/
/.shards/
*.dwarf

# Libraries don't need dependency lock
# Dependencies will be locked in applications that use them
/shard.lock
6 changes: 6 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
language: crystal

# Uncomment the following if you'd like Travis to run specs and check code formatting
# script:
# - crystal spec
# - crystal tool format --check
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2019 Ali Naqvi

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
68 changes: 68 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
# Crystal XZ

Crystal bindings to the XZ (lzma) compression library.

## Installation

1. Add the dependency to your `shard.yml`:

```yaml
dependencies:
xz:
github: naqvis/xz.cr
```
2. Run `shards install`

## Usage

```crystal
require "xz"
```

`XZ` shard provides both `XZ::Reader` and `XZ::Writer`.

## Example: decompress an xz file
#
```crystal
require "xz"
string = File.open("file.xz") do |file|
XZ::Reader.open(file) do |xz|
xz.gets_to_end
end
end
pp string
```

## Example: compress to xz compression format
#
```crystal
require "xz"
File.write("file.txt", "abcd")
File.open("./file.txt", "r") do |input_file|
File.open("./file.xz", "w") do |output_file|
XZ::Writer.open(output_file) do |xz|
IO.copy(input_file, xz)
end
end
end
```

## Development

TODO: Write development instructions here

## Contributing

1. Fork it (<https://github.com/naqvis/xz.cr/fork>)
2. Create your feature branch (`git checkout -b my-new-feature`)
3. Commit your changes (`git commit -am 'Add some feature'`)
4. Push to the branch (`git push origin my-new-feature`)
5. Create a new Pull Request

## Contributors

- [Ali Naqvi](https://github.com/naqvis) - creator and maintainer
11 changes: 11 additions & 0 deletions shard.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
name: xz
version: 0.1.0

authors:
- Ali Naqvi <syed.alinaqvi@gmail.com>
description: |
Crystal bindings to the XZ (lzma) compression library.
crystal: 0.30.1

license: MIT
2 changes: 2 additions & 0 deletions spec/spec_helper.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
require "spec"
require "../src/xz"
34 changes: 34 additions & 0 deletions spec/xz_spec.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
require "./spec_helper"
require "file_utils"
describe XZ do
it "Test Round Trip" do
compressed_io = IO::Memory.new
str = "The quick brown fox jumps over the lazy dog."
XZ::Writer.open(compressed_io) do |zw|
zw.write str.to_slice
end
compressed_io.rewind
uncompressed = XZ::Reader.open(compressed_io) do |zr|
zr.gets_to_end
end
uncompressed.should eq(str)
end

it "Test write/Read file" do
File.open("./LICENSE", "r") do |input_file|
File.open("./LICENSE.xz", "w") do |output_file|
XZ::Writer.open(output_file) do |xz|
IO.copy(input_file, xz)
end
end
end
expected = File.read("./LICENSE")
got = File.open("./LICENSE.xz") do |file|
XZ::Reader.open(file) do |xz|
xz.gets_to_end
end
end
got.should eq(expected)
FileUtils.rm("./LICENSE.xz")
end
end
48 changes: 48 additions & 0 deletions src/xz.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# XZ Crystal Wrapper
require "semantic_version"

module XZ
VERSION = "0.1.0"

LZMA_VERSION = SemanticVersion.parse String.new(LZMA.version_string)
LZMA_VERSION_MINIMUM = SemanticVersion.parse("5.2.4")
raise "unsupported lzma version #{LZMA_VERSION}, needs #{LZMA_VERSION_MINIMUM} or higher" unless LZMA_VERSION >= LZMA_VERSION_MINIMUM

module PRESET
NO_COMPRESSION = 0
BEST_SPEED = 1
BEST_COMPRESSION = 9
DEFAULT_COMPRESSION = 6
end

class CompressionError < Exception
def initialize(ret : LZMA::Ret)
msg = case ret
when .options_error? then "Specific preset is not supported"
when .mem_error? then "Memory allocation failed"
when .data_error? then "File size limits exceeded"
when .unsupported_check? then "Specified integrity check not supported"
else
"Unknown error #{ret}, possible a bug"
end
super("XZ::Writer: #{msg}")
end
end

class DecompressionError < Exception
def initialize(ret : LZMA::Ret)
msg = case ret
when .options_error? then "Unsupported compression options"
when .mem_error? then "Memory allocation failed"
when .format_error? then "File format not recognized. The input is not in the .xz format"
when .data_error? then "Compressed file is corrupt"
when .buf_error? then "Compressed file is truncated or otherwise corrupt"
else
"Unknown error #{ret}, possible a bug"
end
super("XZ::Reader: #{msg}")
end
end
end

require "./xz/*"
Loading

0 comments on commit 2e54dc0

Please sign in to comment.