mirror of
https://github.com/iperov/DeepFaceLive
synced 2025-07-15 01:23:45 -07:00
splitting large files
This commit is contained in:
parent
30cc36c8e4
commit
ee7d471f20
16 changed files with 805308 additions and 7 deletions
76
xlib/file/SplittedFile.py
Normal file
76
xlib/file/SplittedFile.py
Normal file
|
@ -0,0 +1,76 @@
|
|||
import itertools
|
||||
from pathlib import Path
|
||||
from typing import List
|
||||
|
||||
|
||||
class SplittedFile:
|
||||
@staticmethod
|
||||
def split(filepath : Path, part_size : int, delete_original = False):
|
||||
"""
|
||||
splits a file to the parts
|
||||
|
||||
raises:
|
||||
Exception
|
||||
FileNotFoundError
|
||||
"""
|
||||
if part_size == 0:
|
||||
raise Exception(f'part_size == 0')
|
||||
|
||||
if filepath.exists():
|
||||
filesize = filepath.stat().st_size
|
||||
|
||||
n_parts = filesize // part_size
|
||||
|
||||
if filesize - part_size*n_parts != 0:
|
||||
n_parts += 1
|
||||
|
||||
if n_parts > 100:
|
||||
raise Exception('n_parts > 100')
|
||||
|
||||
b = filepath.read_bytes()
|
||||
for n in range(n_parts):
|
||||
part_filepath = filepath.parent / (filepath.name + f'.part{n}')
|
||||
part_filepath.write_bytes(b[n*part_size:(n+1)*part_size])
|
||||
|
||||
|
||||
if delete_original:
|
||||
filepath.unlink()
|
||||
else:
|
||||
raise FileNotFoundError()
|
||||
|
||||
|
||||
@staticmethod
|
||||
def merge(filepath : Path, delete_parts = False):
|
||||
"""
|
||||
if filepath does not exist, merges parts of file if they exist
|
||||
|
||||
example
|
||||
|
||||
filename.ext.part0
|
||||
filename.ext.part1
|
||||
...
|
||||
merged to filename.ext
|
||||
"""
|
||||
if filepath.exists():
|
||||
return
|
||||
|
||||
parts : List[Path] = []
|
||||
for n in itertools.count(start=0):
|
||||
part_filepath = filepath.parent / (filepath.name + f'.part{n}')
|
||||
if part_filepath.exists():
|
||||
parts.append(part_filepath)
|
||||
else:
|
||||
break
|
||||
|
||||
if len(parts) != 0:
|
||||
bytes_parts = []
|
||||
for part_filepath in parts:
|
||||
bytes_parts.append( part_filepath.read_bytes() )
|
||||
|
||||
b = b''.join(bytes_parts)
|
||||
|
||||
filepath.write_bytes(b)
|
||||
|
||||
if delete_parts:
|
||||
for part_filepath in parts:
|
||||
part_filepath.unlink()
|
Loading…
Add table
Add a link
Reference in a new issue