java - How I can split file in Android Case The File Is Large To Upload It(Sound File) -
how can split file in android case file large upload it(sound file) use zip4j how can split when mack zip or way split upload 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 define various properties zipparameters parameters = new zipparameters(); // set compression method deflate compression parameters.setcompressionmethod(zip4jconstants.comp_deflate); //deflate_level_fastest - lowest compression level higher speed of compression //deflate_level_fast - low compression level higher speed of compression //deflate_level_normal - optimal balance between compression level/speed //deflate_level_maximum - high compression level compromise of speed //deflate_level_ultra - highest compression level low speed parameters.setcompressionlevel(zip4jconstants.deflate_level_normal); //set encryption flag true parameters.setencryptfiles(true); //set encryption method aes zip encryption parameters.setencryptionmethod(zip4jconstants.enc_method_aes); //aes_strength_128 - both encryption , decryption //aes_strength_192 - decryption //aes_strength_256 - both encryption , decryption //key strength 192 cannot used encryption. if zip file has //file encrypted key strength of 192, zip4j can decrypt 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 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 1 me.....??
the permission need run process
<uses-permission android:name="android.permission.write_external_storage" /> <uses-permission android:name="android.permission.read_external_storage" />
defined variable locate size of parts....
public static final long floppysize = (long)(1.4 * 1024 * 1024); /** maximum size of each file "chunk" generated, in bytes */ public static long chunksize = (long)(1.4 * 1024 * 1024);
type path of file
string ss = environment.getexternalstoragedirectory().getabsolutepath() + "/download/1.amr"; try { //split(ss); //join(ss); } catch (exception e) { e.printstacktrace(); }
the method split.....
public static void split(string filename) throws filenotfoundexception, ioexception { // open file bufferedinputstream in = new bufferedinputstream(new fileinputstream(filename)); // file length file f = new file(filename); long filesize = f.length(); // loop each full chunk int subfile; (subfile = 0; subfile < filesize / chunksize; subfile++) { // open output file bufferedoutputstream out = new bufferedoutputstream(new fileoutputstream(filename + "." + subfile)); // write right amount of bytes (int currentbyte = 0; currentbyte < chunksize; currentbyte++) { // load 1 byte input file , write output file out.write(in.read()); } // close file out.close(); } // loop last chunk (which may smaller chunk size) if (filesize != chunksize * (subfile - 1)) { // open output file bufferedoutputstream out = new bufferedoutputstream(new fileoutputstream(filename + "." + subfile)); // write rest of file int b; while ((b = in.read()) != -1) out.write(b); // close file out.close(); } // close file in.close(); }
the method join file name only.....
public static void join(string basefilename) throws ioexception { int numberparts = getnumberparts(basefilename); // now, assume files correctly numbered in order (that joker didn't delete part) bufferedoutputstream out = new bufferedoutputstream(new fileoutputstream(basefilename)); (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 number of part.....
private static int getnumberparts(string basefilename) throws ioexception { // list files in 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; }
Comments
Post a Comment