Java分隔、合并大文件

今天网百度网盘上上传文件提示单个文件大小超限,让我升级VIP。作为一个有逼格的程序猿怎么可能被这点小事难倒呢。

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
import com.google.common.collect.Lists;
import org.apache.commons.io.FilenameUtils;

import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.List;

/**
* @author Gao Youbo
* @since 2017-07-07 18:18
*/
public class Files {
public static void main(String[] args) {
int num = 10;

// 分割
cut(new File("/Users/gaoyoubo/360sync/数据迁移/document/a.zip"), num);

// 合并
List<File> files = Lists.newArrayList();
for (int i = 0; i < num; i++) {
files.add(new File("/Users/gaoyoubo/360sync/数据迁移/document/a-" + i + ".zip"));
}
File outFile = new File("/Users/gaoyoubo/360sync/数据迁移/document/b.zip");
merge(files, outFile);
}


/**
* 分文件
*
* @param sourceFile
* @param num 分隔文件数量
*/
public static void cut(File sourceFile, int num) {
long signMaxSize = sourceFile.length() / num + 1; // 单个文件最大长度
try (RandomAccessFile source = new RandomAccessFile(sourceFile, "r")) {
byte[] bytes = new byte[1024];
int len;
for (int i = 0; i < num; i++) {
File targetFile = new File(sourceFile.getParent(),
FilenameUtils.getBaseName(sourceFile.getName()) + "-" + i + "." + FilenameUtils.getExtension(sourceFile.getName()));
try (RandomAccessFile target = new RandomAccessFile(targetFile, "rw")) {
while ((len = source.read(bytes)) != -1) {//读到文件末尾时,len返回-1,结束循环
target.write(bytes, 0, len);
if (target.length() > signMaxSize) {
break;
}
}
}
}
} catch (IOException e) {
e.printStackTrace();
}
}

/**
* 合并文件
*/
public static void merge(List<File> files, File outFile) {
try (RandomAccessFile out = new RandomAccessFile(outFile, "rw")) {
for (File file : files) {
try (RandomAccessFile src = new RandomAccessFile(file, "r")) {
byte[] bytes = new byte[1024];//每次读取字节数
int len;
while ((len = src.read(bytes)) != -1) {
out.write(bytes, 0, len);//循环赋值
}
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}