-
-
Notifications
You must be signed in to change notification settings - Fork 10.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
增加一个文件切割工具 FileSplitUtils #653
Open
UFreedom
wants to merge
2
commits into
Blankj:master
Choose a base branch
from
UFreedom:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
116 changes: 116 additions & 0 deletions
116
utilcode/src/main/java/com/blankj/utilcode/util/FileSplitUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
package com.blankj.utilcode.util; | ||
|
||
import android.os.Build; | ||
import android.support.annotation.RequiresApi; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.io.RandomAccessFile; | ||
import java.nio.channels.FileChannel; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.UUID; | ||
|
||
/** | ||
* <pre> | ||
* author: UFreedom | ||
* blog : http://ufreedom.me | ||
* time : 2018/09/29 | ||
* desc : Util for file split | ||
* </pre> | ||
*/ | ||
@RequiresApi(api = Build.VERSION_CODES.KITKAT) | ||
public class FileSplitUtil { | ||
|
||
|
||
/** | ||
* Split a file into multiples files | ||
* | ||
* @param destCacheDir The dir to be cached for split files | ||
* @param srcFile The file need to be split | ||
* @param expireSplitCount How many files are expected to be cut.The value should at least 2. | ||
* Sometimes a file may not be cut into a specific number.For example, Expire cut into 3, | ||
* but the current file can not be completely cut after cutting 3, there may be a little left, | ||
* and it will eventually be cut into 4 | ||
* @return 切片后的结果 | ||
*/ | ||
public static List<File> splitFile(File destCacheDir, final int expireSplitCount, final File srcFile) throws IOException { | ||
checkDestDir(destCacheDir); | ||
checkSourceFile(srcFile); | ||
if (expireSplitCount < 2) { | ||
throw new IllegalArgumentException("The split count must at least 2"); | ||
} | ||
|
||
final long sourceSize = srcFile.length(); | ||
long bytesPerSplit = sourceSize / expireSplitCount; | ||
return splitFileInner(destCacheDir, srcFile, sourceSize, bytesPerSplit, expireSplitCount); | ||
} | ||
|
||
|
||
/** | ||
* Split a file into multiples files | ||
* | ||
* @param destCacheDir The dir to be cached for split files | ||
* @param srcFile The file need to be split | ||
* @param sliceSize How big is each slice are expected to be cut. Unit(MB) | ||
*/ | ||
public static List<File> splitFile(File destCacheDir, final File srcFile, final int sliceSize) throws IOException { | ||
|
||
checkDestDir(destCacheDir); | ||
checkSourceFile(srcFile); | ||
|
||
if (sliceSize <= 0) { | ||
throw new IllegalArgumentException("The slice size must be more than zero"); | ||
} | ||
|
||
final long sourceSize = srcFile.length(); | ||
final long bytesPerSplit = 1024L * 1024L * sliceSize; | ||
final long numSplits = sourceSize / bytesPerSplit; | ||
|
||
return splitFileInner(destCacheDir, srcFile, sourceSize, bytesPerSplit, numSplits); | ||
} | ||
|
||
private static List<File> splitFileInner(File destDir, File srcFile, long sourceSize, long bytesPerSplit, long numSplits) throws IOException { | ||
List<File> partFiles = new ArrayList<>(); | ||
final long remainingBytes = sourceSize % bytesPerSplit; | ||
int position = 0; | ||
RandomAccessFile sourceFile = new RandomAccessFile(srcFile, "r"); | ||
FileChannel sourceChannel = sourceFile.getChannel(); | ||
for (; position < numSplits; position++) { | ||
writePartToFile(destDir, bytesPerSplit, position * bytesPerSplit, sourceChannel, partFiles); | ||
} | ||
|
||
if (remainingBytes > 0) { | ||
writePartToFile(destDir, remainingBytes, position * bytesPerSplit, sourceChannel, partFiles); | ||
} | ||
return partFiles; | ||
} | ||
|
||
private static void writePartToFile(File destDirFile, long byteSize, long position, FileChannel sourceChannel, List<File> partFiles) throws IOException { | ||
String tempDir = destDirFile.getAbsolutePath().concat("/"); | ||
File file = new File(tempDir + UUID.randomUUID() + "_" + position + "" + ".splitPart"); | ||
RandomAccessFile toFile = new RandomAccessFile(file, "rw"); | ||
FileChannel toChannel = toFile.getChannel(); | ||
sourceChannel.position(position); | ||
toChannel.transferFrom(sourceChannel, 0, byteSize); | ||
partFiles.add(file); | ||
} | ||
|
||
private static void checkSourceFile(File srcFileName) { | ||
if (srcFileName == null || !srcFileName.exists()) { | ||
throw new NullPointerException("The source file should be non-empty or exist"); | ||
} | ||
} | ||
|
||
private static void checkDestDir(File cacheDir) { | ||
if (cacheDir == null || !cacheDir.exists()) { | ||
throw new NullPointerException("The cache directory should exist"); | ||
} | ||
|
||
if (!cacheDir.isDirectory()) { | ||
throw new IllegalArgumentException("The cache directory should not be a file"); | ||
} | ||
} | ||
|
||
|
||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
是不是这样可读性更强?