使用 tar、gz、zip 或 bzip2 拆分文件 [已关闭]

问题描述 投票:0回答:4

我需要压缩大约 17-20 GB 的大文件。我需要将其分成几个文件,每个文件大约 1 GB。

我通过 Google 搜索了解决方案,并找到了使用 splitcat 命令的方法。但它们根本不适用于大文件。此外,它们不能在 Windows 中运行;我需要在 Windows 机器上解压它。

linux bash file-io compression
4个回答
264
投票

您可以使用

split
命令和
-b
选项:

split -b 1024m file.tar.gz

可以使用@Joshua的答案在Windows机器上重新组装。

copy /b file1 + file2 + file3 + file4 filetogether

编辑:正如@Charlie在下面的评论中所述,您可能需要明确设置一个前缀,因为否则它将使用

x
,这可能会令人困惑。

split -b 1024m "file.tar.gz" "file.tar.gz.part-"

// Creates files: file.tar.gz.part-aa, file.tar.gz.part-ab, file.tar.gz.part-ac, ...

编辑:编辑帖子,因为问题已结束,最有效的解决方案与此答案的内容非常接近:

# create archives
$ tar cz my_large_file_1 my_large_file_2 | split -b 1024MiB - myfiles_split.tgz_
# uncompress
$ cat myfiles_split.tgz_* | tar xz

此解决方案避免了在压缩(解压缩)时使用中间大文件的需要。使用 tar -C 选项为生成的文件使用不同的目录。顺便说一句,如果存档仅由单个文件组成,则可以避免 tar 并仅使用 gzip:

# create archives
$ gzip -c my_large_file | split -b 1024MiB - myfile_split.gz_
# uncompress
$ cat myfile_split.gz_* | gunzip -c > my_large_file

对于 Windows,您可以下载相同命令的移植版本或使用 cygwin。


29
投票

如果您是从 Linux 中拆分出来的,您仍然可以在 Windows 中重新组装。

copy /b file1 + file2 + file3 + file4 filetogether

10
投票

使用 tar 拆分为多个存档

有很多程序可以在 Windows 上处理 tar 文件,包括 Cygwin


9
投票

经过测试的代码,最初创建一个存档文件,然后将其拆分:

 gzip -c file.orig > file.gz
 CHUNKSIZE=1073741824
 PARTCNT=$[$(stat -c%s file.gz) / $CHUNKSIZE]

 # the remainder is taken care of, for example for
 # 1 GiB + 1 bytes PARTCNT is 1 and seq 0 $PARTCNT covers
 # all of file
 for n in `seq 0 $PARTCNT`
 do
       dd if=file.gz of=part.$n bs=$CHUNKSIZE skip=$n count=1
 done

此变体省略创建单个存档文件并直接创建零件:

gzip -c file.orig |
    ( CHUNKSIZE=1073741824;
        i=0;
        while true; do
            i=$[i+1];
            head -c "$CHUNKSIZE" > "part.$i";
            [ "$CHUNKSIZE" -eq $(stat -c%s "part.$i") ] || break;
        done; )

在此变体中,如果存档的文件大小可被

$CHUNKSIZE
整除,则最后一个部分文件的文件大小将为 0 字节。

© www.soinside.com 2019 - 2024. All rights reserved.