package org.example.Movie.DownloadZipFiles;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.StringTokenizer;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import org.example.Movie.DataBaseHelper;
import org.example.Movie.SharedVariables;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
public class DonloadZipFileAndUnzipIt {
private static SQLiteDatabase dataBase=null;
static ArrayList<String> zipURLs=null;
private String _zipFile;
private String _location;
public DonloadZipFileAndUnzipIt(){}
public DonloadZipFileAndUnzipIt(String zipFile, String location) {
_zipFile = zipFile;
_location = location;
_dirChecker("");
}
public void unzip() {
try {
FileInputStream fin = new FileInputStream(_zipFile);
ZipInputStream zin = new ZipInputStream(fin);
ZipEntry ze = null;
while ((ze = zin.getNextEntry()) != null) {
Log.v("Decompress", "Unzipping " + ze.getName());
System.out.println("^^^^^^UnzippingFile^"+ze.getName());
///code to search is given string exists or not in a Sentence
String haystack = ze.getName();
String needle1 = ".DS_Store";
int index1 = haystack.indexOf(needle1);
if (index1 != -1)
{
System.out.println("The string contains the substring " + needle1);
continue;
}
/*else
System.out.println("The string does not contain the substring " + needle1);*/
if(ze.isDirectory()) {
_dirChecker(ze.getName());
} else {
FileOutputStream fout = new FileOutputStream(_location + ze.getName());
// replace for loop with:
byte[] buffer = new byte[1024];
int length;
while ((length = zin.read(buffer))>0) {
fout.write(buffer, 0, length);
}
zin.closeEntry();
fout.close();
}
}////Outer While
zin.close();
} catch(Exception e) {
Log.e("Decompress", "unzip", e);
}
}
private void _dirChecker(String dir) {
File f = new File(_location + dir);
if(!f.isDirectory()) {
f.mkdirs();
}
}
public void downLoadZipFiles(File filePath,String zipURLs)
{
//ArrayList<String> zipURLs=getURLList();
String lastString=null;
StringTokenizer st=null;
String folderName=null;
/*for(int i=0;i<zipURLs.size();i++)
{*/
URL url = null;
try {
System.out.println("***zipURLs****"+zipURLs);
System.out.println("***zipURLs****"+zipURLs);
///code to devide a String into different parts based on given symbol (eg:'/')..
String str =zipURLs; ///"
http://9.dharani.org/Upendra/M_T_MAHESH.zip";
st = new StringTokenizer(str, "/");
System.out.println("Tokens Count: " + st.countTokens());
String arrayString[]=new String[st.countTokens()];
int tokenCount=st.countTokens();
for(int k=0;k<tokenCount;k++)
{
arrayString[k]=st.nextToken();
lastString=arrayString[k];
System.out.println("lastString : " + lastString);
}
st = new StringTokenizer(lastString, ".");
folderName=st.nextToken();
url = new URL(zipURLs);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
URLConnection conexion = url.openConnection();
conexion.connect();
int lenghtOfFile = conexion.getContentLength();
Log.d("ANDRO_ASYNC", "Lenght of file: " + lenghtOfFile);
InputStream input = new BufferedInputStream(url.openStream());
//OutputStream output = new FileOutputStream("/sdcard/foldername/temp.zip");
//OutputStream output = new FileOutputStream(filePath+"/"+folderName+"/"+lastString);
OutputStream output = new FileOutputStream(filePath+"/"+lastString);
try {
int aReasonableSize = 1024;
byte[] buffer = new byte[aReasonableSize];
int bytesRead = 0;
while ((bytesRead = input.read(buffer, 0, buffer.length)) >= 0) {
output.write(buffer, 0, bytesRead);//buffer.length);
}
} //try
finally {
output.flush();
output.close();
input.close();
}//finally
///code to UnzipFile
String zipFile = filePath+"/"+lastString;/////Environment.getExternalStorageDirectory() + "/files.zip";
String unzipLocation = filePath + "/"+folderName+"/";
DonloadZipFileAndUnzipIt d = new DonloadZipFileAndUnzipIt(zipFile, unzipLocation);
d.unzip();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//File fXmlFile = new File(filePath+"/zipFiles/Category.xml");
/*}//for...
*/
}
public static ArrayList<String> getURLList()
{
zipURLs=new ArrayList<String>();
DataBaseHelper dbHelper =new DataBaseHelper(SharedVariables.globalContext);
dataBase = dbHelper.getReadableDatabase();
Cursor CategoryURLCursor = dataBase.rawQuery("select *from Category",null);
if (CategoryURLCursor.getCount() > 0) {
while (CategoryURLCursor.moveToNext()) {
//add all values to ArrayList
zipURLs.add(CategoryURLCursor.getString(CategoryURLCursor.getColumnIndex("CategoryURL")));
}
}
dataBase.close();
dbHelper.close();
return zipURLs;
}
}
to execute above code u just simply call Below method using object of above class:
downloadObj.downLoadZipFiles(filePath,str);
where
File filePath=Environment.getExternalStorageDirectory(); (or) getFilesDir();
String str=URL of ZipFile u want to Download and Unzip
Comments
Post a Comment