Skip to content

Commit

Permalink
refactor: StringUtil & FileUtil
Browse files Browse the repository at this point in the history
  • Loading branch information
zyyzyykk committed Dec 6, 2024
1 parent c74d0a2 commit ee8a2ac
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public static void fileChunkMerge(String folderPath, String fileName, Integer ch
List<File> list = new ArrayList<>();
for (File file : files) {
// 判断是否是文件对应的文件片
if (StringUtil.isFileChunk(file.getName(),chunks,fileName)) {
if (isFileChunk(file.getName(),chunks,fileName)) {
list.add(file);
}
}
Expand All @@ -39,8 +39,8 @@ public static void fileChunkMerge(String folderPath, String fileName, Integer ch
}
// 根据切片文件的下标进行排序
List<File> fileListCollect = list.parallelStream().sorted(((file1, file2) -> {
Integer chunk1 = StringUtil.getFileChunkIndex(file1.getName());
Integer chunk2 = StringUtil.getFileChunkIndex(file2.getName());
Integer chunk1 = getFileChunkIndex(file1.getName());
Integer chunk2 = getFileChunkIndex(file2.getName());
return chunk1 - chunk2;
})).collect(Collectors.toList());
// 根据排序的顺序依次将文件合并到新的文件中
Expand Down Expand Up @@ -80,4 +80,38 @@ public static void tmpFloderDelete(File directory) {
directory.delete();
}


/**
* 判断分片文件名
*/
private static Boolean isFileChunk(String chunkFileName, Integer chunks, String originFileName) {
int index = chunkFileName.lastIndexOf("-");
if(index != -1) {
String fileName = chunkFileName.substring(0,index);
if(!originFileName.equals(fileName)) return false;
try {
int chunk = Integer.parseInt(chunkFileName.substring(index + 1));
if(chunk < 1 || chunk > chunks) return false;
} catch (Exception e) {
return false;
}
return true;
}

return false;
}


/**
* 获取文件片片号
*/
private static Integer getFileChunkIndex(String chunkFileName) {
int index = chunkFileName.lastIndexOf("-");
if(index != -1) {
return Integer.parseInt(chunkFileName.substring(index + 1));
}

return 1;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -43,36 +43,4 @@ public static String changeStr(String str) {
return result.toString();
}

/**
* 判断分片文件名
*/
public static Boolean isFileChunk(String chunkFileName, Integer chunks, String originFileName) {
int index = chunkFileName.lastIndexOf("-");
if(index != -1) {
String fileName = chunkFileName.substring(0,index);
if(!originFileName.equals(fileName)) return false;
try {
int chunk = Integer.parseInt(chunkFileName.substring(index + 1));
if(chunk < 1 || chunk > chunks) return false;
} catch (Exception e) {
return false;
}
return true;
}

return false;
}

/**
* 获取文件片片号
*/
public static Integer getFileChunkIndex(String chunkFileName) {
int index = chunkFileName.lastIndexOf("-");
if(index != -1) {
return Integer.parseInt(chunkFileName.substring(index + 1));
}

return 1;
}

}

0 comments on commit ee8a2ac

Please sign in to comment.