Upload compress/compress.py with huggingface_hub
Browse files- compress/compress.py +63 -0
compress/compress.py
ADDED
@@ -0,0 +1,63 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import shutil
|
3 |
+
import math
|
4 |
+
from tqdm import tqdm
|
5 |
+
|
6 |
+
def split_and_compress_directory(src_dir, dst_base_dir, num_parts=5):
|
7 |
+
"""
|
8 |
+
将源目录分割成多个等大小的部分并压缩
|
9 |
+
|
10 |
+
Args:
|
11 |
+
src_dir (str): 源目录路径
|
12 |
+
dst_base_dir (str): 目标基础目录
|
13 |
+
num_parts (int): 要分割的部分数量
|
14 |
+
"""
|
15 |
+
# 确保目标目录存在
|
16 |
+
os.makedirs(dst_base_dir, exist_ok=True)
|
17 |
+
|
18 |
+
# 获取所有文件路径
|
19 |
+
all_files = []
|
20 |
+
for dirpath, _, filenames in os.walk(src_dir):
|
21 |
+
for f in filenames:
|
22 |
+
all_files.append(os.path.join(dirpath, f))
|
23 |
+
|
24 |
+
# 计算每部分的文件数量
|
25 |
+
files_per_part = math.ceil(len(all_files) / num_parts)
|
26 |
+
|
27 |
+
# 分割并压缩
|
28 |
+
for i in range(num_parts):
|
29 |
+
part_name = f"part_{i+1}"
|
30 |
+
start_idx = i * files_per_part
|
31 |
+
end_idx = min((i + 1) * files_per_part, len(all_files))
|
32 |
+
|
33 |
+
# 创建当前部分的文件列表
|
34 |
+
current_files = all_files[start_idx:end_idx]
|
35 |
+
|
36 |
+
print(f"\n处理第 {i+1}/{num_parts} 部分...")
|
37 |
+
|
38 |
+
# 使用tqdm显示进度
|
39 |
+
for src_file in tqdm(current_files, desc=f"复制文件到 {part_name}"):
|
40 |
+
# 保持目录结构
|
41 |
+
rel_path = os.path.relpath(src_file, src_dir)
|
42 |
+
dst_file = os.path.join(dst_base_dir, part_name, rel_path)
|
43 |
+
|
44 |
+
# 确保目标目录存在
|
45 |
+
os.makedirs(os.path.dirname(dst_file), exist_ok=True)
|
46 |
+
|
47 |
+
# 复制文件
|
48 |
+
shutil.copy2(src_file, dst_file)
|
49 |
+
|
50 |
+
# 压缩当前部分
|
51 |
+
part_dir = os.path.join(dst_base_dir, part_name)
|
52 |
+
shutil.make_archive(part_dir, 'zip', part_dir)
|
53 |
+
|
54 |
+
# 删除未压缩的目录
|
55 |
+
shutil.rmtree(part_dir)
|
56 |
+
|
57 |
+
print(f"完成 {part_name} 的压缩")
|
58 |
+
|
59 |
+
# 使用示例
|
60 |
+
src_directory = "/mnt/HDD_16TB/jusheng_files/Align3D-130/objaverse10/images"
|
61 |
+
dst_directory = "/mnt/HDD_16TB/jusheng_files/Align3D-130/objaverse10/img_compress"
|
62 |
+
|
63 |
+
split_and_compress_directory(src_directory, dst_directory, 5)
|