Java 如何在Android环境下拆分文件文件文件较大,以便上传(声音文件)
如何在Android环境下拆分文件文件文件较大,以便上传(声音文件) 我使用zip4j,但我如何在压缩时拆分它,或者以任何方式拆分以在压缩时上载我的代码Java 如何在Android环境下拆分文件文件文件较大,以便上传(声音文件),java,android,Java,Android,如何在Android环境下拆分文件文件文件较大,以便上传(声音文件) 我使用zip4j,但我如何在压缩时拆分它,或者以任何方式拆分以在压缩时上载我的代码 private static void compress(String inputFile, String compressedFile) { try { ZipFile zipFile = new ZipFile(compressedFile); File inputFileH = new File(i
private static void compress(String inputFile, String compressedFile) {
try {
ZipFile zipFile = new ZipFile(compressedFile);
File inputFileH = new File(inputFile);
//Initiate Zip Parameters which define various properties
ZipParameters parameters = new ZipParameters();
// set compression method to deflate compression
parameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE);
//DEFLATE_LEVEL_FASTEST - Lowest compression level but higher speed of compression
//DEFLATE_LEVEL_FAST - Low compression level but higher speed of compression
//DEFLATE_LEVEL_NORMAL - Optimal balance between compression level/speed
//DEFLATE_LEVEL_MAXIMUM - High compression level with a compromise of speed
//DEFLATE_LEVEL_ULTRA - Highest compression level but low speed
parameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL);
//Set the encryption flag to true
parameters.setEncryptFiles(true);
//Set the encryption method to AES Zip Encryption
parameters.setEncryptionMethod(Zip4jConstants.ENC_METHOD_AES);
//AES_STRENGTH_128 - For both encryption and decryption
//AES_STRENGTH_192 - For decryption only
//AES_STRENGTH_256 - For both encryption and decryption
//Key strength 192 cannot be used for encryption. But if a zip file already has a
//file encrypted with key strength of 192, then Zip4j can decrypt this file
parameters.setAesKeyStrength(Zip4jConstants.AES_STRENGTH_256);
// file compressed
zipFile.addFile(inputFileH, parameters);
long uncompressedSize = inputFileH.length();
File outputFileH = new File(compressedFile);
long comrpessedSize = outputFileH.length();
//System.out.println("Size "+uncompressedSize+" vs "+comrpessedSize);
double ratio = (double) comrpessedSize / (double) uncompressedSize;
System.out.println("File compressed with compression ratio : " + ratio);
} catch (Exception e) {
e.printStackTrace();
}
}
public static void decompress(String compressedFile, String destination) {
try {
ZipFile zipFile = new ZipFile(compressedFile);
if (zipFile.isEncrypted()) {
zipFile.setPassword("123");
}
zipFile.extractAll(destination);
} catch (ZipException e) {
e.printStackTrace();
}
System.out.println("File
Decompressed");
}
}
谁能帮我…?运行该进程所需的权限
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
键入文件的路径
String ss = Environment.getExternalStorageDirectory().getAbsolutePath() + "/Download/1.amr";
try {
//split(ss);
//join(ss);
}
catch (Exception e) {
e.printStackTrace();
}
拆分的方法
public static void split(String filename) throws FileNotFoundException, IOException
{
// open the file
BufferedInputStream in = new BufferedInputStream(new FileInputStream(filename));
// get the file length
File f = new File(filename);
long fileSize = f.length();
// loop for each full chunk
int subfile;
for (subfile = 0; subfile < fileSize / chunkSize; subfile++)
{
// open the output file
BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(filename + "." + subfile));
// write the right amount of bytes
for (int currentByte = 0; currentByte < chunkSize; currentByte++)
{
// load one byte from the input file and write it to the output file
out.write(in.read());
}
// close the file
out.close();
}
// loop for the last chunk (which may be smaller than the chunk size)
if (fileSize != chunkSize * (subfile - 1))
{
// open the output file
BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(filename + "." + subfile));
// write the rest of the file
int b;
while ((b = in.read()) != -1)
out.write(b);
// close the file
out.close();
}
// close the file
in.close();
}
运行该进程所需的权限
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
键入文件的路径
String ss = Environment.getExternalStorageDirectory().getAbsolutePath() + "/Download/1.amr";
try {
//split(ss);
//join(ss);
}
catch (Exception e) {
e.printStackTrace();
}
拆分的方法
public static void split(String filename) throws FileNotFoundException, IOException
{
// open the file
BufferedInputStream in = new BufferedInputStream(new FileInputStream(filename));
// get the file length
File f = new File(filename);
long fileSize = f.length();
// loop for each full chunk
int subfile;
for (subfile = 0; subfile < fileSize / chunkSize; subfile++)
{
// open the output file
BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(filename + "." + subfile));
// write the right amount of bytes
for (int currentByte = 0; currentByte < chunkSize; currentByte++)
{
// load one byte from the input file and write it to the output file
out.write(in.read());
}
// close the file
out.close();
}
// loop for the last chunk (which may be smaller than the chunk size)
if (fileSize != chunkSize * (subfile - 1))
{
// open the output file
BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(filename + "." + subfile));
// write the rest of the file
int b;
while ((b = in.read()) != -1)
out.write(b);
// close the file
out.close();
}
// close the file
in.close();
}
请显示示例代码请显示示例代码