Reputation: 101
How I can split file in Android Case The File Is Large To Upload It(Sound File) I use zip4j but how i can split it when Mack it zip or any way to split To upload It this my code when zip
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");
}
}
can any one help me.....??
Upvotes: 1
Views: 3464
Reputation: 101
The Permission You Need to Run The Process
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
Defined This Variable To Locate Size Of Parts....
public static final long floppySize = (long)(1.4 * 1024 * 1024);
/** the maximum size of each file "chunk" generated, in bytes */
public static long chunkSize = (long)(1.4 * 1024 * 1024);
Type The Path Of Your File
String ss = Environment.getExternalStorageDirectory().getAbsolutePath() + "/Download/1.amr";
try {
//split(ss);
//join(ss);
}
catch (Exception e) {
e.printStackTrace();
}
The Method To Split.....
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();
}
The Method To Join By File Name Only.....
public static void join(String baseFilename) throws IOException
{
int numberParts = getNumberParts(baseFilename);
// now, assume that the files are correctly numbered in order (that some joker didn't delete any part)
BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(baseFilename));
for (int part = 0; part < numberParts; part++)
{
BufferedInputStream in = new BufferedInputStream(new FileInputStream(baseFilename + "." + part));
int b;
while ( (b = in.read()) != -1 )
out.write(b);
in.close();
}
out.close();
}
The Method To Get Number Of Part.....
private static int getNumberParts(String baseFilename) throws IOException
{
// list all files in the same directory
File directory = new File(baseFilename).getAbsoluteFile().getParentFile();
final String justFilename = new File(baseFilename).getName();
String[] matchingFiles = directory.list(new FilenameFilter()
{
public boolean accept(File dir, String name)
{
return name.startsWith(justFilename) && name.substring(justFilename.length()).matches("^\\.\\d+$");
}
});
return matchingFiles.length;
}
Upvotes: 9