File: zip_file_spec.cr

package info (click to toggle)
crystal 1.6.0%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 18,956 kB
  • sloc: javascript: 1,712; sh: 592; cpp: 541; makefile: 243; ansic: 119; python: 105; xml: 32
file content (132 lines) | stat: -rw-r--r-- 3,226 bytes parent folder | download
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
require "../../spec_helper"
require "compress/zip"

describe Compress::Zip do
  it "reads file from memory" do
    io = IO::Memory.new

    Compress::Zip::Writer.open(io) do |zip|
      zip.add "foo.txt", "contents of foo"
      zip.add "bar.txt", "contents of bar"
    end

    io.rewind

    Compress::Zip::File.open(io) do |zip|
      entries = zip.entries
      entries.size.should eq(2)

      foo = entries[0]
      foo.filename.should eq("foo.txt")

      bar = entries[1]
      bar.filename.should eq("bar.txt")

      zip["foo.txt"].filename.should eq("foo.txt")
      zip["bar.txt"].filename.should eq("bar.txt")
      zip["baz.txt"]?.should be_nil

      foo.open do |foo_io|
        bar.open do |bar_io|
          foo_io.gets_to_end.should eq("contents of foo")
          bar_io.gets_to_end.should eq("contents of bar")
        end
      end
    end
  end

  it "reads file from file system" do
    filename = datapath("file.zip")

    begin
      File.open(filename, "w") do |file|
        Compress::Zip::Writer.open(file) do |zip|
          zip.add "foo.txt", "contents of foo"
          zip.add "bar.txt", "contents of bar"
        end
      end

      File.open(filename, "r") do |file|
        Compress::Zip::File.open(file) do |zip|
          entries = zip.entries
          entries.size.should eq(2)

          foo = entries[0]
          foo.filename.should eq("foo.txt")

          bar = entries[1]
          bar.filename.should eq("bar.txt")

          zip["foo.txt"].filename.should eq("foo.txt")
          zip["bar.txt"].filename.should eq("bar.txt")
          zip["baz.txt"]?.should be_nil

          foo.open do |foo_io|
            bar.open do |bar_io|
              foo_io.gets_to_end.should eq("contents of foo")
              bar_io.gets_to_end.should eq("contents of bar")
            end
          end
        end
      end
    ensure
      File.delete(filename)
    end
  end

  it "writes comment" do
    io = IO::Memory.new

    Compress::Zip::Writer.open(io) do |zip|
      zip.add Compress::Zip::Writer::Entry.new("foo.txt", comment: "some comment"),
        "contents of foo"
    end

    io.rewind

    Compress::Zip::File.open(io) do |zip|
      zip["foo.txt"].comment.should eq("some comment")
    end
  end

  it "reads big file" do
    io = IO::Memory.new

    Compress::Zip::Writer.open(io) do |zip|
      100.times do |i|
        zip.add "foo#{i}.txt", "some contents #{i}"
      end
    end

    io.rewind

    Compress::Zip::File.open(io) do |zip|
      zip.entries.size.should eq(100)
    end
  end

  it "reads zip file with different extra in local file header and central directory header" do
    Compress::Zip::File.open(datapath("test.zip")) do |zip|
      zip.entries.size.should eq(2)
      zip["one.txt"].open(&.gets_to_end).should eq("One")
      zip["two.txt"].open(&.gets_to_end).should eq("Two")
    end
  end

  it "reads zip comment" do
    io = IO::Memory.new

    Compress::Zip::Writer.open(io) do |zip|
      zip.comment = "zip comment"
    end

    io.rewind

    Compress::Zip::File.open(io) do |zip|
      zip.comment.should eq("zip comment")
    end
  end

  typeof(Compress::Zip::File.new("file.zip"))
  typeof(Compress::Zip::File.open("file.zip") { })
end