-
Notifications
You must be signed in to change notification settings - Fork 3
/
libhjsplit.py
93 lines (89 loc) · 3.61 KB
/
libhjsplit.py
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
########################################################################
# This program is free software: you can redistribute it and/or modify #
# it under the terms of the GNU General Public License as published by #
# the Free Software Foundation, either version 3 of the License, or #
# (at your option) any later version. #
# #
# This program is distributed in the hope that it will be useful, #
# but WITHOUT ANY WARRANTY; without even the implied warranty of #
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
# GNU General Public License for more details. #
# #
# You should have received a copy of the GNU General Public License #
# along with this program. If not, see <http://www.gnu.org/licenses/>.#
# #
# Copyright (C) 2013 by marcomg #
########################################################################
import os
# Provide to copy a content of a file in another file (using buffer and a limit to copy)
def __copyInFile(iF, oF, buffersize=1024, tocopy = 0):
copied = 0
i = 0
while True:
i += 1
elsetocpy = tocopy - copied
# free to copy all
if (elsetocpy - buffersize > 0) or (tocopy == 0):
tmp = iF.read(buffersize)
if tmp == b'':
if i == 1:
return False
else:
return True
else:
oF.write(tmp)
copied += buffersize
# last data to copy
else:
tmp = iF.read(elsetocpy)
if tmp == b'':
if i == 1:
return False
else:
return True
else:
oF.write(tmp)
return True
# Split files
def split(inFileSrc, output, splitIn):
splitNumber = 1
try:
inFile = open(inFileSrc, 'rb');
except FileNotFoundError:
print('Error: the file %s does not exists. Exiting...' % (inFileSrc))
exit()
while True:
if output == None:
outFile = open(inFileSrc + '.' + str('%03d' % (splitNumber)), 'wb')
else:
outFile = open(os.path.join(output, os.path.basename(inFileSrc)) + '.' + str('%03d' % (splitNumber)), 'wb')
if not __copyInFile(inFile, outFile, 1024, splitIn):
outFile.close()
if output == None:
os.remove(inFileSrc + '.' + str('%03d' % (splitNumber)))
else:
os.remove(os.path.join(output, os.path.basename(inFileSrc)) + '.' + str('%03d' % (splitNumber)))
break
else:
outFile.close()
splitNumber += 1
# Join files
def join(firstFileIn, output):
firstFileInE = firstFileIn[:-3]
outFileSrc = firstFileInE[:-1]
joinNumber = 1
if output != None:
outFileSrc = os.path.join(output, os.path.basename(outFileSrc))
outFile = open(outFileSrc, 'wb')
while True:
try:
inFile = open(firstFileInE + str('%03d' % (joinNumber)), 'rb')
__copyInFile(inFile, outFile, 1024, 0)
#outFile.write(inFile.read())
inFile.close()
joinNumber += 1
except FileNotFoundError:
if joinNumber <= 1:
print('Error: the file %s.001 does not exists. Exiting...' % (firstFileIn))
outFile.close()
return