-
Notifications
You must be signed in to change notification settings - Fork 1
/
tarball.cr
73 lines (58 loc) · 1.54 KB
/
tarball.cr
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# ```crystal
# require "tarball"
#
# # Extracts all files in "archive.tar" to under the "data" directory.
# Tarball.extract_all("archive.tar", "data")
# ```
module Tarball
VERSION = "0.1.4"
# :nodoc:
class Error < Exception; end
# :nodoc:
class ArchiveError < Error; end
# :nodoc:
class EntryError < Error; end
# :nodoc:
class HeaderError < Error; end
# :nodoc:
class EntityError < Error; end
# :nodoc:
class PaxdataError < Error; end
BLOCK_SIZE = 512
# :nodoc:
END_OF_ARCHIVE = Bytes.new(BLOCK_SIZE, 0)
# Extracts all file system objects in the tar archive file.
def self.extract_all(archive_file : String, dir = ".")
Archive.open(archive_file) do |archive|
archive.extract_all(dir)
end
end
# Opens tar archive file.
def self.open(archive_file : String)
Archive.open(archive_file)
end
# Opens tar archive file and yields given block.
def self.open(archive_file : String, &block)
archive = Archive.open(archive_file)
yield archive
archive.close
end
# Opens gzipped tar archive file.
def self.open_gz(archive_file : String)
GzArchive.open(archive_file)
end
# Opens gzipped tar archive file and yields given block.
def self.open_gz(archive_file : String, &block)
archive = GzArchive.open(archive_file)
yield archive
archive.close
end
# :nodoc:
def self.read_block(io : IO)
block = Bytes.new(BLOCK_SIZE, 0)
read_size = io.read(block)
raise Error.new("Invalid file size") unless read_size == BLOCK_SIZE
block
end
end
require "./tarball/*"