rabbit_file_split 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: e86777a0e6221919dce99c7434435dafbde3a84a
4
+ data.tar.gz: bd4437ffcf7cb1de06049344d4c35e8a2686eea1
5
+ SHA512:
6
+ metadata.gz: 8da9a3dc1789e4284922899875a9a73b3f271dfd25b4012bf8c61031b79116d0184186a78bbfb49937dcbf76b372c46a1004a85fa907eb7887b3a7de247e4d1c
7
+ data.tar.gz: ed898962d9e9412be03837cd71a6df6d8d9ec944f15df4f31fce94dc7ff9b87ac4bd3ab66ff70f11340f5ea0307f9cbc2a37c009eb663dfc5de5d823304b478f
data/.gitignore ADDED
@@ -0,0 +1,15 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
15
+ .idea/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in rabbit_file_split.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 AKB428
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,37 @@
1
+ # RabbitFileSplit
2
+
3
+ Simple File Byte Split
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'rabbit_file_split'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install rabbit_file_split
20
+
21
+ ## Usage
22
+
23
+ #1M(1048576)byte split
24
+ split_file_list = RabbitFileSplit::Bytes.new(file_path).split(1048576)
25
+
26
+ split_file_list = RabbitFileSplit::Bytes.new(file_path).split(1048576, file_prefix: 'object')
27
+ split_file_list = RabbitFileSplit::Bytes.new(file_path).split(1048576, file_prefix: 'object', dest_file_path: '/tmp')
28
+ split_file_list = RabbitFileSplit::Bytes.new(file_path).split(1048576, file_prefix: 'object', dest_file_path: '/tmp', buffer_size: 8096)
29
+
30
+
31
+ ## Contributing
32
+
33
+ 1. Fork it ( https://github.com/[my-github-username]/rabbit_file_split/fork )
34
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
35
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
36
+ 4. Push to the branch (`git push origin my-new-feature`)
37
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
data/bin/split.rb ADDED
@@ -0,0 +1,8 @@
1
+ require 'rabbit_file_split'
2
+
3
+ #ruby -I./lib/ bin/split.rb
4
+
5
+ rabbit_file_split = RabbitFileSplit::Bytes.new(ARGV[0])
6
+
7
+ #ex 1048576 (1MB)
8
+ rabbit_file_split.split(ARGV[1].to_i, dest_file_path: ARGV[2])
@@ -0,0 +1,93 @@
1
+ require 'digest/md5'
2
+
3
+ module RabbitFileSplit
4
+ class Bytes
5
+
6
+ # bytes_split (uintmax_t n_bytes, char *buf, size_t bufsize, size_t initial_read, uintmax_t max_files)
7
+
8
+ BUFFER_SIZE = 4096
9
+
10
+ @file_path = nil
11
+ @file_base_name = nil
12
+ @split_file_num = 0
13
+ @file_list = Array.new
14
+ @file_parent_path = nil
15
+ @original_file_size = nil
16
+ @split_work_time = nil
17
+
18
+ def initialize(file_path)
19
+ @file_path = file_path
20
+ @file_base_name = File::basename(file_path)
21
+ @file_parent_path = File::dirname(file_path)
22
+ @original_file_size = File.size(file_path)
23
+ end
24
+
25
+ def split(n_bytes, file_prefix: @file_base_name, dest_file_path: @file_parent_path, buffer_size: BUFFER_SIZE)
26
+ start_time = Time.now
27
+
28
+ total_readed_byte = 0
29
+ unit_readed_byte = 0
30
+ @split_file_num = 1
31
+ io = File.open(@file_path, 'rb')
32
+ w_file_io = File.open(create_split_file_path(dest_file_path,file_prefix,@split_file_num), 'wb')
33
+ while(true)
34
+ buffer = io.read(buffer_size)
35
+ #puts buffer_size
36
+ total_readed_byte += buffer_size
37
+ unit_readed_byte += buffer_size
38
+ w_file_io.write(buffer)
39
+
40
+ #check total readed
41
+ if (total_readed_byte + buffer_size) > @original_file_size
42
+ total_rest_byte = @original_file_size - total_readed_byte
43
+ if ( (unit_readed_byte + total_rest_byte) > n_bytes )
44
+ # n_bytes over. file change
45
+ rest_byte = n_bytes - unit_readed_byte
46
+ if rest_byte > 0
47
+ rest_buffer = io.read(rest_byte)
48
+ w_file_io.write(rest_buffer)
49
+ end
50
+ w_file_io.close
51
+ @split_file_num +=1
52
+ #file change
53
+ w_file_io = File.open(create_split_file_path(dest_file_path,file_prefix,@split_file_num), 'wb')
54
+ end
55
+ # read rest byte
56
+ if total_rest_byte > 0
57
+ rest_buffer = io.read(total_rest_byte)
58
+ w_file_io.write(rest_buffer)
59
+ end
60
+
61
+ end_time = Time.now
62
+ @split_work_time = end_time - start_time
63
+ return @file_list
64
+ end
65
+
66
+ #check n_bytes
67
+ if (unit_readed_byte + buffer_size) > n_bytes
68
+ rest_byte = n_bytes - unit_readed_byte
69
+ if rest_byte > 0
70
+ rest_buffer = io.read(rest_byte)
71
+ w_file_io.write(rest_buffer)
72
+ end
73
+ w_file_io.close
74
+ @split_file_num +=1
75
+ #file change
76
+ unit_readed_byte = 0
77
+ w_file_io = File.open(create_split_file_path(dest_file_path,file_prefix,@split_file_num), 'wb')
78
+ end
79
+ end
80
+ end
81
+
82
+ private
83
+
84
+ def create_split_file_path(dest_file_path, file_prefix, num)
85
+ file_path = File.join(dest_file_path, file_prefix + '_' + sprintf('%04d', num))
86
+ @file_list = [] if @file_list.nil?
87
+ @file_list.push(file_path)
88
+ puts file_path
89
+ file_path
90
+ end
91
+
92
+ end
93
+ end
@@ -0,0 +1,3 @@
1
+ module RabbitFileSplit
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,7 @@
1
+ require "rabbit_file_split/version"
2
+ require "rabbit_file_split/bytes"
3
+
4
+ module RabbitFileSplit
5
+
6
+
7
+ end
@@ -0,0 +1,23 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'rabbit_file_split/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "rabbit_file_split"
8
+ spec.version = RabbitFileSplit::VERSION
9
+ spec.authors = ["AKB428"]
10
+ spec.email = ["otoraru@gmail.com"]
11
+ spec.summary = %q{simple file byte split}
12
+ spec.description = %q{file byte split module}
13
+ spec.homepage = "https://github.com/AKB428"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.7"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+ end
metadata ADDED
@@ -0,0 +1,83 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rabbit_file_split
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - AKB428
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-02-12 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.7'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.7'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ description: file byte split module
42
+ email:
43
+ - otoraru@gmail.com
44
+ executables:
45
+ - split.rb
46
+ extensions: []
47
+ extra_rdoc_files: []
48
+ files:
49
+ - ".gitignore"
50
+ - Gemfile
51
+ - LICENSE.txt
52
+ - README.md
53
+ - Rakefile
54
+ - bin/split.rb
55
+ - lib/rabbit_file_split.rb
56
+ - lib/rabbit_file_split/bytes.rb
57
+ - lib/rabbit_file_split/version.rb
58
+ - rabbit_file_split.gemspec
59
+ homepage: https://github.com/AKB428
60
+ licenses:
61
+ - MIT
62
+ metadata: {}
63
+ post_install_message:
64
+ rdoc_options: []
65
+ require_paths:
66
+ - lib
67
+ required_ruby_version: !ruby/object:Gem::Requirement
68
+ requirements:
69
+ - - ">="
70
+ - !ruby/object:Gem::Version
71
+ version: '0'
72
+ required_rubygems_version: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - ">="
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ requirements: []
78
+ rubyforge_project:
79
+ rubygems_version: 2.2.2
80
+ signing_key:
81
+ specification_version: 4
82
+ summary: simple file byte split
83
+ test_files: []