Skip to content

Commit

Permalink
Fix Compress::Gzip extra field (#14550)
Browse files Browse the repository at this point in the history
  • Loading branch information
kojix2 committed Jul 6, 2024
1 parent 074ec99 commit 45c9e6f
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 3 deletions.
16 changes: 15 additions & 1 deletion spec/std/compress/gzip/gzip_spec.cr
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
require "spec"
require "../../spec_helper"
require "compress/gzip"

private SAMPLE_TIME = Time.utc(2016, 1, 2)
Expand Down Expand Up @@ -57,4 +57,18 @@ describe Compress::Gzip do
gzip.rewind
gzip.gets_to_end.should eq(SAMPLE_CONTENTS)
end

it "reads file with extra fields from file system" do
File.open(datapath("test.gz")) do |file|
Compress::Gzip::Reader.open(file) do |gzip|
header = gzip.header.not_nil!
header.modification_time.to_utc.should eq(Time.utc(2012, 9, 4, 22, 6, 5))
header.os.should eq(3_u8)
header.extra.should eq(Bytes[1, 2, 3, 4, 5])
header.name.should eq("test.txt")
header.comment.should eq("happy birthday")
gzip.gets_to_end.should eq("One\nTwo")
end
end
end
end
Binary file added spec/std/data/test.gz
Binary file not shown.
4 changes: 2 additions & 2 deletions src/compress/gzip/header.cr
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class Compress::Gzip::Header
@os = header[9]

if flg.extra?
xlen = io.read_byte.not_nil!
xlen = io.read_bytes(UInt16, IO::ByteFormat::LittleEndian)
@extra = Bytes.new(xlen)
io.read_fully(@extra)
end
Expand Down Expand Up @@ -86,7 +86,7 @@ class Compress::Gzip::Header
io.write_byte os

unless @extra.empty?
io.write_byte @extra.size.to_u8
io.write_bytes(@extra.size.to_u16, IO::ByteFormat::LittleEndian)
io.write(@extra)
end

Expand Down

0 comments on commit 45c9e6f

Please sign in to comment.