zipstream 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (5) hide show
  1. data/LICENSE +20 -0
  2. data/README.md +11 -0
  3. data/VERSION +1 -0
  4. data/lib/zipstream.rb +127 -0
  5. metadata +61 -0
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2010-2011 Samuel Cochran
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,11 @@
1
+ # ZipStream
2
+
3
+ Create zip files to a stream.
4
+
5
+ ## Thanks
6
+
7
+ Inspired by http://pablotron.org/software/zipstream-php/
8
+
9
+ ## License
10
+
11
+ Copyright (c) 2011 Samuel Cochran (sj26@sj26.com). Released under the MIT License, see [LICENSE][license] for details.
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.1
data/lib/zipstream.rb ADDED
@@ -0,0 +1,127 @@
1
+ require 'date'
2
+ require 'zlib'
3
+
4
+ # Simple, streamable zip file creation
5
+ class ZipStream
6
+ VERSION = File.read File.expand_path '../../VERSION', __FILE__
7
+
8
+ def initialize stream, options={}
9
+ @stream = stream
10
+ @length = 0
11
+ @directory = ""
12
+ @directory_count = 0
13
+ @directory_length = 0
14
+ @options = options
15
+ end
16
+
17
+ def write name, data, options={}
18
+ # Fails without specifying the window bits (odd!)
19
+ deflater = Zlib::Deflate.new Zlib::BEST_COMPRESSION, -Zlib::MAX_WBITS
20
+ zdata = deflater.deflate data, Zlib::FINISH
21
+ deflater.close
22
+
23
+ method = 0x08
24
+ timestamp = dostime options[:datetime] || DateTime.now
25
+ crc = Zlib.crc32 data
26
+ zdata_length = zdata.length
27
+ data_length = data.length
28
+ name_length = name.length
29
+ comment = options[:comment].to_s || ""
30
+ comment_length = comment.length
31
+
32
+ @directory << [
33
+ # central file header signature
34
+ 0x02014b50,
35
+ # version made by
36
+ (6 << 8) + 3,
37
+ # version needed to extract
38
+ (6 << 8) + 3,
39
+ # general purpose bit flag
40
+ 0x00,
41
+ # compresion method (deflate or store)
42
+ method,
43
+ # dos timestamp
44
+ timestamp,
45
+ # crc32 of data
46
+ crc,
47
+ # compressed data length
48
+ zdata_length,
49
+ # uncompressed data length
50
+ data_length,
51
+ # filename length
52
+ name_length,
53
+ # extra data len
54
+ 0,
55
+ # file comment length
56
+ comment_length,
57
+ # disk number start
58
+ 0,
59
+ # internal file attributes
60
+ 0,
61
+ # external file attributes
62
+ 32,
63
+ # relative offset of local header
64
+ @length,
65
+ ].pack('Vv4V4v5V2') << name << comment
66
+ @directory_length += 46 + name_length + comment_length
67
+ @directory_count += 1
68
+
69
+ @stream << [
70
+ # local file header signature
71
+ 0x04034b50,
72
+ # version needed to extract
73
+ (6 << 8) + 3,
74
+ # general purpose bit flag
75
+ 0x00,
76
+ # compresion method (deflate or store)
77
+ method,
78
+ # dos timestamp
79
+ timestamp,
80
+ # checksum of data
81
+ crc,
82
+ # compressed data length
83
+ zdata_length,
84
+ # uncompressed data length
85
+ data_length,
86
+ # filename length
87
+ name_length,
88
+ # extra data len
89
+ 0,
90
+ ].pack('Vv3V4v2') << name << zdata
91
+ @length += 30 + name_length + zdata_length
92
+ end
93
+
94
+ # TODO: #write_file
95
+
96
+ # TODO: use storage large files
97
+
98
+ def close
99
+ comment = @options[:comment].to_s || ""
100
+ comment_length = comment.length
101
+
102
+ @stream << @directory << [
103
+ # end of central file header signature
104
+ 0x06054b50,
105
+ # this disk number
106
+ 0x00,
107
+ # number of disk with cdr
108
+ 0x00,
109
+ # number of entries in the cdr on this disk
110
+ @directory_count,
111
+ # number of entries in the cdr
112
+ @directory_count,
113
+ # cdr size
114
+ @directory_length,
115
+ # cdr ofs
116
+ @length,
117
+ # zip file comment length
118
+ comment_length,
119
+ ].pack('Vv4V2v') << comment
120
+ end
121
+
122
+ protected
123
+
124
+ def dostime datetime
125
+ datetime.year << 25 | datetime.month << 21 | datetime.day << 16 | datetime.hour << 11 | datetime.minute << 5 | datetime.second >> 1
126
+ end
127
+ end
metadata ADDED
@@ -0,0 +1,61 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: zipstream
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Samuel Cochran
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2011-11-04 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rspec
16
+ requirement: &70290903330240 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: 2.7.0
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: *70290903330240
25
+ description:
26
+ email: sj26@sj26.com
27
+ executables: []
28
+ extensions: []
29
+ extra_rdoc_files:
30
+ - README.md
31
+ - LICENSE
32
+ files:
33
+ - README.md
34
+ - LICENSE
35
+ - VERSION
36
+ - lib/zipstream.rb
37
+ homepage: http://github.com/sj26/zipstream
38
+ licenses: []
39
+ post_install_message:
40
+ rdoc_options: []
41
+ require_paths:
42
+ - lib
43
+ required_ruby_version: !ruby/object:Gem::Requirement
44
+ none: false
45
+ requirements:
46
+ - - ! '>='
47
+ - !ruby/object:Gem::Version
48
+ version: '0'
49
+ required_rubygems_version: !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ requirements: []
56
+ rubyforge_project:
57
+ rubygems_version: 1.8.11
58
+ signing_key:
59
+ specification_version: 3
60
+ summary: Create zip files directly to a stream.
61
+ test_files: []