add lzma support
This commit is contained in:
@@ -2,8 +2,8 @@ dest: /path/to/store/backup/files # The programs will store database and backup
|
|||||||
enable_pcre2: false # Optional. Default value: false. Try to use PCRE2 first. PCRE2 may be a little slower than internal regex library.
|
enable_pcre2: false # Optional. Default value: false. Try to use PCRE2 first. PCRE2 may be a little slower than internal regex library.
|
||||||
remove_old_files: true # Optional. Default value: true. Remove unneeded backup files which already deleted in source tree when backuping files.
|
remove_old_files: true # Optional. Default value: true. Remove unneeded backup files which already deleted in source tree when backuping files.
|
||||||
ignore_hidden_files: true # Optional. Default value: true. Whether to ignore files which its name starts with ".". Only effect folder which type is "path".
|
ignore_hidden_files: true # Optional. Default value: true. Whether to ignore files which its name starts with ".". Only effect folder which type is "path".
|
||||||
compress_method: "bzip2" # Optional. Default value: null. Supported value: "bzip2", "gzip"
|
compress_method: "bzip2" # Optional. Default value: null. Supported value: "bzip2", "gzip", "lzma"
|
||||||
compress_level: 6 # Optional. Default value: null. bzip2 support 1-9 (Default: 9). gzip support 0-9 (Default: 9).
|
compress_level: 6 # Optional. Default value: null. bzip2 support 1-9 (Default: 9). gzip support 0-9 (Default: 9). lzma support 0-9 (Default: 6).
|
||||||
programs:
|
programs:
|
||||||
- name: Your program name # This name is used to identify different application.
|
- name: Your program name # This name is used to identify different application.
|
||||||
base: /path/to/save/path # Must be absoulte path.
|
base: /path/to/save/path # Must be absoulte path.
|
||||||
|
|||||||
@@ -8,6 +8,11 @@ try:
|
|||||||
have_gzip = True
|
have_gzip = True
|
||||||
except ImportError:
|
except ImportError:
|
||||||
have_gzip = False
|
have_gzip = False
|
||||||
|
try:
|
||||||
|
from lzma import LZMAFile
|
||||||
|
have_lzma = True
|
||||||
|
except ImportError:
|
||||||
|
have_lzma = False
|
||||||
from enum import IntEnum, unique
|
from enum import IntEnum, unique
|
||||||
try:
|
try:
|
||||||
from functools import cached_property
|
from functools import cached_property
|
||||||
@@ -21,6 +26,7 @@ from os import remove
|
|||||||
class CompressMethod(IntEnum):
|
class CompressMethod(IntEnum):
|
||||||
BZIP2 = 0
|
BZIP2 = 0
|
||||||
GZIP = 1
|
GZIP = 1
|
||||||
|
LZMA = 2
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def from_str(v: str) -> IntEnum:
|
def from_str(v: str) -> IntEnum:
|
||||||
@@ -30,6 +36,8 @@ class CompressMethod(IntEnum):
|
|||||||
return CompressMethod.BZIP2
|
return CompressMethod.BZIP2
|
||||||
elif t == "gzip":
|
elif t == "gzip":
|
||||||
return CompressMethod.GZIP
|
return CompressMethod.GZIP
|
||||||
|
elif t == "lzma":
|
||||||
|
return CompressMethod.LZMA
|
||||||
else:
|
else:
|
||||||
raise TypeError('Must be str.')
|
raise TypeError('Must be str.')
|
||||||
|
|
||||||
@@ -61,6 +69,17 @@ class CompressConfig:
|
|||||||
else:
|
else:
|
||||||
raise ValueError('gzip: compress_level should be 0-9.')
|
raise ValueError('gzip: compress_level should be 0-9.')
|
||||||
self._ext = '.gz'
|
self._ext = '.gz'
|
||||||
|
elif self._method == CompressMethod.LZMA:
|
||||||
|
if not have_lzma:
|
||||||
|
raise NotImplementedError("lzma not supported.")
|
||||||
|
if level is None:
|
||||||
|
self._level = 6
|
||||||
|
else:
|
||||||
|
if isinstance(level, int) and level >= 0 and level <= 9:
|
||||||
|
self._level = level
|
||||||
|
else:
|
||||||
|
raise ValueError('lzma: compress_level should be 0-9.')
|
||||||
|
self._ext = ".xz"
|
||||||
self._chunk_size = 1048576
|
self._chunk_size = 1048576
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
@@ -89,6 +108,8 @@ if have_bz2:
|
|||||||
supported_exts.append('.bz2')
|
supported_exts.append('.bz2')
|
||||||
if have_gzip:
|
if have_gzip:
|
||||||
supported_exts.append('.gz')
|
supported_exts.append('.gz')
|
||||||
|
if have_lzma:
|
||||||
|
supported_exts.append('.xz')
|
||||||
|
|
||||||
|
|
||||||
def sizeof_fmt(num, suffix='B'):
|
def sizeof_fmt(num, suffix='B'):
|
||||||
@@ -126,6 +147,14 @@ def compress(src: str, dest: str, c: CompressConfig, name: str, prog: str):
|
|||||||
f.write(a)
|
f.write(a)
|
||||||
a = t.read(cs)
|
a = t.read(cs)
|
||||||
del a
|
del a
|
||||||
|
elif c.method == CompressMethod.LZMA:
|
||||||
|
with open(src, 'rb') as t:
|
||||||
|
with LZMAFile(fn, 'wb', preset=c.level) as f:
|
||||||
|
a = t.read(cs)
|
||||||
|
while a != b'':
|
||||||
|
f.write(a)
|
||||||
|
a = t.read(cs)
|
||||||
|
del a
|
||||||
i = compress_info(getsize(src), getsize(fn))
|
i = compress_info(getsize(src), getsize(fn))
|
||||||
print(f'{prog}: Compressed {src}({name}) -> {fn} ({i})')
|
print(f'{prog}: Compressed {src}({name}) -> {fn} ({i})')
|
||||||
del i
|
del i
|
||||||
@@ -157,5 +186,13 @@ def decompress(src: str, dest: str, c: CompressConfig, name: str, prog: str):
|
|||||||
t.write(a)
|
t.write(a)
|
||||||
a = f.read(cs)
|
a = f.read(cs)
|
||||||
del a
|
del a
|
||||||
|
elif c.method == CompressMethod.LZMA:
|
||||||
|
with LZMAFile(fn, 'rb') as f:
|
||||||
|
with open(dest, 'wb') as t:
|
||||||
|
a = f.read(cs)
|
||||||
|
while a != b'':
|
||||||
|
t.write(a)
|
||||||
|
a = f.read(cs)
|
||||||
|
del a
|
||||||
i = compress_info(getsize(dest), getsize(fn))
|
i = compress_info(getsize(dest), getsize(fn))
|
||||||
print(f'{prog}: Decompressed {fn}({name}) -> {dest} ({i})')
|
print(f'{prog}: Decompressed {fn}({name}) -> {dest} ({i})')
|
||||||
|
|||||||
Reference in New Issue
Block a user