Skip to content
This repository was archived by the owner on Mar 20, 2025. It is now read-only.

Commit 45c8315

Browse files
committed
allow gzip onix file
1 parent e539ea0 commit 45c8315

File tree

4 files changed

+52
-11
lines changed

4 files changed

+52
-11
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ Or install it yourself as:
2626

2727
`onix3 extract onixfile.xml 3325246 --comment` does the same as above but commenting the product extracted.
2828

29+
`onix3 count onixfile.xml` returns ne number of products in the XML (counting the parts that shouldn't be sold independently)
30+
31+
Onix file could be plain text XML or a gzip archive file
2932

3033
## Contributing
3134

bin/onix3

Lines changed: 47 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,16 @@
22

33
require 'onix3'
44
require 'optitron'
5+
require 'zlib'
56

67
class Runner < Optitron::CLI
78

9+
class_opt "gzip", "Source file is gziped"
10+
811
desc "Comment an onix 3 file"
912
# arg "file", "The file to comment"
1013
def comment(onix_file)
11-
File.open(onix_file, "rb") do |file|
14+
io_for_onix(onix_file) do |file|
1215
p = Onix3::Parser::Divider.new(file)
1316
puts p.document_start
1417
c = Onix3::Tools::Commenter.new
@@ -30,7 +33,7 @@ class Runner < Optitron::CLI
3033
# arg "identifier", "Identifier of the product to extract"
3134
opt "comment", "Comment the returned file", :type => :boolean, :use_no => true
3235
def extract(onix_file, product_identifier)
33-
File.open(onix_file, "rb") do |file|
36+
io_for_onix(onix_file) do |file|
3437
p = Onix3::Parser::Divider.new(file)
3538
puts p.document_start
3639
c = Onix3::Tools::Commenter.new
@@ -44,16 +47,51 @@ class Runner < Optitron::CLI
4447

4548
end
4649

47-
=begin
48-
cmd "count", "Count products in the onix 3 file" do
49-
arg "file", "The onix 3 file"
50+
51+
desc "Count products in the onix 3 file"
52+
# arg "file", "The onix 3 file"
53+
def count(onix_file)
54+
io_for_onix(onix_file) do |file|
55+
p = Onix3::Parser::Divider.new(file)
56+
count = 0
57+
p.each_product do |product|
58+
count += 1
59+
end
60+
puts count
61+
end
5062
end
51-
cmd "list", "list product identifiers in an onix 3 file" do
52-
opt "types", "list of identifiers types to report", type: :array
53-
arg "file", "The onix 3 file"
63+
64+
protected
65+
66+
def io_for_onix(onix_file)
67+
File.open(onix_file, "rb") do |f|
68+
gzip = (f.read(2) == "\x1f\x8b".b)
69+
f.rewind
70+
if gzip
71+
z = Zlib::GzipReader.new(f, :encoding => 'binary')
72+
yield z
73+
z.close
74+
else
75+
yield f
76+
end
77+
end
78+
end
79+
80+
=begin
81+
q
82+
desc "List product identifiers in an onix 3 file"
83+
opt "types", "list of identifiers types to report", type: :array
84+
def list(onix_file)
85+
File.open(onix_file, "rb") do |file|
86+
p = Onix3::Parser::Divider.new(file)
87+
p.each_product do |product|
88+
#
89+
end
90+
end
5491
end
55-
5692
=end
93+
94+
5795

5896
end
5997

lib/onix3/parser/base.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ class Base
55
attr_writer :reader
66

77
def initialize(io)
8-
io.binmode
8+
io.binmode if io.respond_to? :binmode
99
@io = io
1010
end
1111

lib/onix3/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
module Onix3
2-
VERSION = "0.0.3"
2+
VERSION = "0.0.4"
33
end

0 commit comments

Comments
 (0)