i have implemented download of files. downloading , saving memory.
now problem is, if there folder , sub folder, need zip folder , download zip file , save in memory. tried lot did'nt find solution. can 1 can guide me solution?
here download single file code...
@override protected string doinbackground(string... surl) { inputstream input = null; outputstream output = null; zipoutputstream zos = null; httpurlconnection connection = null; try { url url = new url(surl[0]); connection = (httpurlconnection) url.openconnection(); connection.connect(); if (connection.getresponsecode() != httpurlconnection.http_ok) { return "server returned http " + connection.getresponsecode() + " " + connection.getresponsemessage(); } string filename = tvtitle.gettext().tostring(); string fileextension = tvtype.gettext().tostring(); file imagedirectory = new file(path); imagedirectory.mkdirs(); int filelength = connection.getcontentlength(); string _path = path; input = connection.getinputstream(); file outputfile = new file(_path, filename + fileextension); output = new fileoutputstream(outputfile); // zos = new zipoutputstream(output); byte data[] = new byte[4096]; long total = 0; int count; while ((count = input.read(data)) != -1) { if (iscancelled()) { input.close(); return null; } total += count; if (filelength > 0) // if total length known publishprogress((int) (total * 100 / filelength)); output.write(data, 0, count); } } catch (exception e) { return e.tostring(); } { try { if (output != null) output.close(); if (input != null) input.close(); } catch (ioexception ignored) { } if (connection != null) connection.disconnect(); } return null; }
first add permission in manifest
<uses-permission android:name="android.permission.write_external_storage"/>
call method passing "inputfolderpath" , "outputfolderpath"
private static void zipfolder(string inputfolderpath, string outzippath) { try { fileoutputstream fos = new fileoutputstream(outzippath); zipoutputstream zos = new zipoutputstream(fos); file srcfile = new file(inputfolderpath); file[] files = srcfile.listfiles(); log.d("", "zip directory: " + srcfile.getname()); (int = 0; < files.length; i++) { log.d("", "adding file: " + files[i].getname()); byte[] buffer = new byte[1024]; fileinputstream fis = new fileinputstream(files[i]); zos.putnextentry(new zipentry(files[i].getname())); int length; while ((length = fis.read(buffer)) > 0) { zos.write(buffer, 0, length); } zos.closeentry(); fis.close(); } zos.close(); } catch (ioexception ioe) { log.e("", ioe.getmessage()); } }
Comments
Post a Comment