Class: Zipstream

Inherits:
Object
  • Object
show all
Defined in:
lib/zipstream.rb,
lib/zipstream/version.rb

Overview

Simple, streamable zip file creation

Defined Under Namespace

Classes: Body, FiberYieldingStream, Railtie

Constant Summary collapse

Fiber =

Use native Fibers if available

Fiber
VERSION =
'0.1.3'

Instance Method Summary collapse

Constructor Details

#initialize(stream, options = {}) ⇒ Zipstream

Returns a new instance of Zipstream.



6
7
8
9
10
11
12
13
# File 'lib/zipstream.rb', line 6

def initialize stream, options={}
  @stream = stream
  @length = 0
  @directory = ""
  @directory_count = 0
  @directory_length = 0
  @options = options
end

Instance Method Details

#closeObject

TODO: use storage large files



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/zipstream.rb', line 102

def close
  comment = @options[:comment].to_s || ""
  comment_length = comment.length

  @stream << @directory << [
    # end of central file header signature
    0x06054b50,
    # this disk number
    0x00,
    # number of disk with cdr
    0x00,
    # number of entries in the cdr on this disk
    @directory_count,
    # number of entries in the cdr
    @directory_count,
    # cdr size
    @directory_length,
    # cdr ofs
    @length,
    # zip file comment length
    comment_length,
  ].pack('Vv4V2v') << comment
end

#write(name, data, options = {}) ⇒ Object



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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/zipstream.rb', line 15

def write name, data, options={}
  if options[:method] == :store
    zdata = data
    method = 0x00
  else
    # Fails without specifying the window bits (odd!)
    deflater = Zlib::Deflate.new Zlib::BEST_COMPRESSION, -Zlib::MAX_WBITS
    zdata = deflater.deflate data, Zlib::FINISH
    deflater.close
    method = 0x08
  end

  timestamp = dostime options[:datetime] || DateTime.now
  crc = Zlib.crc32 data
  zdata_length = zdata.length
  data_length = data.length
  name = name.to_s
  name_length = name.length
  comment = options[:comment].to_s || ""
  comment_length = comment.length

  @directory << [
    # central file header signature
    0x02014b50,
    # version made by - "DOS" & http://www.pkware.com/documents/casestudies/APPNOTE.TXT Version: 6.3.2
    (0 << 8) & 63,
    # version needed to extract - "DOS" & "2.0 - File is compressed using Deflate compression"
    (0 << 8) & 20,
    # general purpose bit flag
    0x00,
    # compresion method (deflate or store)
    method,
    # dos timestamp
    timestamp,
    # crc32 of data
    crc,
    # compressed data length
    zdata_length,
    # uncompressed data length
    data_length,
    # filename length
    name_length,
    # extra data len
    0,
    # file comment length
    comment_length,
    # disk number start
    0,
    # internal file attributes
    0,
    # external file attributes
    32,
    # relative offset of local header
    @length,
  ].pack('Vv4V4v5V2') << name << comment
  @directory_length += 46 + name_length + comment_length
  @directory_count += 1

  @stream << [
    # local file header signature
    0x04034b50,
    # version needed to extract - "DOS" & "2.0 - File is compressed using Deflate compression"
    (0 << 8) & 20,
    # general purpose bit flag
    0x00,
    # compresion method (deflate or store)
    method,
    # dos timestamp
    timestamp,
    # checksum of data
    crc,
    # compressed data length
    zdata_length,
    # uncompressed data length
    data_length,
    # filename length
    name_length,
    # extra data len
    0,
  ].pack('Vv3V4v2') << name << zdata
  @length += 30 + name_length + zdata_length
end