Created
March 20, 2017 14:33
-
-
Save initpwn/1e1a38b4abbd2abd838947d26448b719 to your computer and use it in GitHub Desktop.
Delta++,BCA Proj.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* To change this template, choose Tools | Templates | |
* and open the template in the editor. | |
*/ | |
package delta; | |
import ie.wombat.jbdiff.JBDiff; | |
import java.awt.TrayIcon; | |
import java.io.BufferedInputStream; | |
import java.io.BufferedOutputStream; | |
import java.io.BufferedReader; | |
import java.io.BufferedWriter; | |
import java.io.File; | |
import java.io.FileInputStream; | |
import java.io.FileOutputStream; | |
import java.io.FileReader; | |
import java.io.FileWriter; | |
import java.io.IOException; | |
import java.io.InputStream; | |
import java.io.OutputStream; | |
import java.io.PrintWriter; | |
import java.util.ArrayList; | |
import java.util.jar.JarOutputStream; | |
import java.util.logging.Level; | |
import java.util.logging.Logger; | |
import java.util.zip.ZipEntry; | |
import java.util.zip.ZipInputStream; | |
import java.util.zip.ZipOutputStream; | |
import javax.servlet.ServletException; | |
import javax.servlet.http.HttpServlet; | |
import javax.servlet.http.HttpServletRequest; | |
import javax.servlet.http.HttpServletResponse; | |
import javax.swing.JOptionPane; | |
import org.itadaki.bzip2.BZip2OutputStream; | |
/** | |
* | |
* @author User | |
*/ | |
public class Getnewapk extends HttpServlet { | |
DB db = new DB(); | |
private File oldfile; | |
private File newfile; | |
private String PatchFolderDir; | |
private File DecomprsdOldVesionFile; | |
private File DecomprsdNewVesionFile; | |
private String OldVersionOutputFolder; | |
private String NewVersionOutputFolder; | |
private String NewVersionManifestFolder; | |
private ArrayList<String> NewFileManifestName; | |
private ArrayList<String> OldFileManifestName; | |
private ArrayList<String> NewFileManifestSHA; | |
private ArrayList<String> OldFileManifestSHA; | |
private ArrayList<String> NewFiles; | |
private ArrayList<String> UpdatedFiles; | |
private ArrayList<String> SameFiles; | |
private ArrayList<String> DeletedFiles; | |
private String patchfilepath; | |
/** | |
* Processes requests for both HTTP | |
* <code>GET</code> and | |
* <code>POST</code> methods. | |
* | |
* @param request servlet request | |
* @param response servlet response | |
* @throws ServletException if a servlet-specific error occurs | |
* @throws IOException if an I/O error occurs | |
*/ | |
protected void processRequest(HttpServletRequest request, HttpServletResponse response) | |
throws ServletException, IOException { | |
response.setContentType("text/html;charset=UTF-8"); | |
PrintWriter out = response.getWriter(); | |
String pakagename = request.getParameter("Name"); | |
String pakageversion = request.getParameter("Version"); | |
System.out.println("Nmae :" + pakagename); | |
System.out.println("Version :" + pakageversion); | |
try { | |
if (db.oldVersionApkExist(pakagename, pakageversion)) { | |
System.out.println("OLD Version Exist"); | |
String oldfilepath = db.getOldApkPath(pakagename, pakageversion); | |
String newfilepath = db.getNewApkPath(pakagename); | |
oldfile = new File(oldfilepath); | |
newfile = new File(newfilepath); | |
System.out.println("New APK PAth : "+newfilepath); | |
PatchFolderDir = newfile.getParent() + "\\" + newfile.getName() + "_PATCH\\"; | |
if (oldfile.isFile() && newfile.isFile()) { | |
String OldVersionName = oldfile.getAbsolutePath(); | |
String NewVersionName = newfile.getAbsolutePath(); | |
OldVersionName = getFileNameWithOutExtn(OldVersionName); | |
NewVersionName = getFileNameWithOutExtn(NewVersionName); | |
DecomprsdOldVesionFile = new File(OldVersionName + ".zip"); | |
DecomprsdNewVesionFile = new File(NewVersionName + ".zip"); | |
oldfile.renameTo(DecomprsdOldVesionFile); | |
newfile.renameTo(DecomprsdNewVesionFile); | |
OldVersionOutputFolder = DecomprsdOldVesionFile.getParent() + "\\" + getFileNameWithOutExtn(DecomprsdOldVesionFile.getName()) + "\\"; | |
NewVersionOutputFolder = DecomprsdNewVesionFile.getParent() + "\\" + getFileNameWithOutExtn(DecomprsdNewVesionFile.getName()) + "\\";; | |
unZipIt(DecomprsdOldVesionFile); | |
unZipIt(DecomprsdNewVesionFile); | |
DecomprsdOldVesionFile.renameTo(oldfile); | |
DecomprsdNewVesionFile.renameTo(newfile); | |
String OldVersionManifestFolder = OldVersionOutputFolder + "META-INF\\"; | |
NewVersionManifestFolder = NewVersionOutputFolder + "META-INF\\"; | |
NewFileManifestName = new ArrayList<String>(); | |
OldFileManifestName = new ArrayList<String>(); | |
NewFileManifestSHA = new ArrayList<String>(); | |
OldFileManifestSHA = new ArrayList<String>(); | |
System.out.println("old meta folder :" + OldVersionManifestFolder); | |
System.out.println("new meta folder :" + NewVersionManifestFolder); | |
{ | |
BufferedReader br = null; | |
try { | |
String sCurrentLine; | |
br = new BufferedReader(new FileReader(OldVersionManifestFolder + "MANIFEST.MF")); | |
int last = 0; | |
while ((sCurrentLine = br.readLine()) != null) { | |
// MetaData MD=new MetaData(); | |
// System.out.println("sCurrnet:>>>"+sCurrentLine); | |
if (sCurrentLine.contains(":")) { | |
int l = sCurrentLine.indexOf(":"); | |
String attribute = sCurrentLine.substring(0, l); | |
if (attribute.contains("Name")) { | |
String atrrValue = sCurrentLine.substring(l + 1, sCurrentLine.length()).trim(); | |
//System.out.println("Name:>"+atrrValue); | |
OldFileManifestName.add(atrrValue); | |
last = 1; | |
} else if (attribute.contains("SHA1-Digest")) { | |
String atrrValue = sCurrentLine.substring(l + 1, sCurrentLine.length()).trim(); | |
// System.out.println("SHA:>"+atrrValue); | |
OldFileManifestSHA.add(atrrValue); | |
last = 2; | |
} | |
} else if (sCurrentLine.trim().length() > 0) { | |
if (last == 1) { | |
int lastName = OldFileManifestName.size(); | |
System.out.println("Name error :>" + OldFileManifestName.get(lastName - 1)); | |
String Name = OldFileManifestName.get(lastName - 1) + sCurrentLine.trim(); | |
OldFileManifestName.remove(lastName - 1); | |
OldFileManifestName.add(Name); | |
last = 0; | |
System.out.println("Name error :fixed >" + Name); | |
} else if (last == 2) { | |
int lastSHA = OldFileManifestSHA.size(); | |
System.out.println("SHA error :>" + OldFileManifestName.get(lastSHA - 1)); | |
String SHA = OldFileManifestSHA.get(lastSHA - 1) + sCurrentLine.trim(); | |
OldFileManifestSHA.remove(lastSHA - 1); | |
OldFileManifestSHA.add(SHA); | |
last = 0; | |
System.out.println("Name error :fixed >" + SHA); | |
} | |
} | |
//System.out.println(sCurrentLine); | |
} | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} finally { | |
try { | |
if (br != null) { | |
br.close(); | |
} | |
} catch (IOException ex) { | |
ex.printStackTrace(); | |
} | |
} | |
} | |
{ | |
BufferedReader br = null; | |
try { | |
String sCurrentLine; | |
br = new BufferedReader(new FileReader(NewVersionManifestFolder + "MANIFEST.MF")); | |
int last = 0; | |
while ((sCurrentLine = br.readLine()) != null) { | |
// MetaData MD=new MetaData(); | |
if (sCurrentLine.contains(":")) { | |
int l = sCurrentLine.indexOf(":"); | |
String attribute = sCurrentLine.substring(0, l); | |
if (attribute.contains("Name")) { | |
String atrrValue = sCurrentLine.substring(l + 1, sCurrentLine.length()).trim(); | |
//System.out.println("Name:>"+atrrValue); | |
NewFileManifestName.add(atrrValue); | |
last = 1; | |
} else if (attribute.contains("SHA1-Digest")) { | |
String atrrValue = sCurrentLine.substring(l + 1, sCurrentLine.length()).trim(); | |
// System.out.println("SHA:>"+atrrValue); | |
NewFileManifestSHA.add(atrrValue); | |
last = 2; | |
} | |
//NewFileManifest.add(MD); | |
} else if (sCurrentLine.trim().length() > 0) { | |
//This code block tho... invoking on latest versions of APKs.... | |
//Getting name error.... IDK... Please explain. | |
if (last == 1) { | |
int lastName = NewFileManifestName.size(); | |
System.out.println("Name error :>" + NewFileManifestName.get(lastName - 1)); | |
String Name = NewFileManifestName.get(lastName - 1) + sCurrentLine.trim(); | |
NewFileManifestName.remove(lastName - 1); | |
NewFileManifestName.add(Name); | |
last = 0; | |
System.out.println("Name error :fixed >" + Name); | |
} else if (last == 2) { | |
int lastSHA = NewFileManifestSHA.size(); | |
System.out.println("SHA error :>" + NewFileManifestName.get(lastSHA - 1)); | |
String SHA = NewFileManifestSHA.get(lastSHA - 1) + sCurrentLine.trim(); | |
NewFileManifestSHA.remove(lastSHA - 1); | |
NewFileManifestSHA.add(SHA); | |
last = 0; | |
System.out.println("Name error :fixed >" + SHA); | |
} | |
} | |
//System.out.println(sCurrentLine); | |
} | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} finally { | |
try { | |
if (br != null) { | |
br.close(); | |
} | |
} catch (IOException ex) { | |
ex.printStackTrace(); | |
} | |
} | |
} | |
NewFiles = new ArrayList<String>(); | |
UpdatedFiles = new ArrayList<String>(); | |
SameFiles = new ArrayList<String>(); | |
DeletedFiles = new ArrayList<String>(); | |
for (int i = 0; i < NewFileManifestName.size(); i++) { | |
if (!OldFileManifestName.contains(NewFileManifestName.get(i))) { | |
NewFiles.add(NewFileManifestName.get(i)); | |
} else if (OldFileManifestName.contains(NewFileManifestName.get(i))) { | |
if (!OldFileManifestSHA.contains(NewFileManifestSHA.get(i))) { | |
UpdatedFiles.add(NewFileManifestName.get(i)); | |
} else if (OldFileManifestSHA.contains(NewFileManifestSHA.get(i))) { | |
SameFiles.add(NewFileManifestName.get(i)); | |
} else { | |
} | |
} | |
} | |
for (int i = 0; i < OldFileManifestName.size(); i++) { | |
if (!NewFileManifestName.contains(OldFileManifestName.get(i))) { | |
DeletedFiles.add(OldFileManifestName.get(i)); | |
} | |
} | |
System.out.println("Number of files in old version :" + OldFileManifestName.size()); | |
System.out.println("Number of files in new version :" + NewFileManifestName.size()); | |
System.out.println("Number of files having tag new :" + NewFiles.size()); | |
System.out.println("Number of files having tag updated :" + UpdatedFiles.size()); | |
System.out.println("Number of files having tag same :" + SameFiles.size()); | |
System.out.println("Number of files having tag deleted :" + DeletedFiles.size()); | |
if (PatchFolderDir != null) { | |
System.out.println("" + PatchFolderDir); | |
File PatchFolder = new File(PatchFolderDir); | |
if (!PatchFolder.exists()) { | |
PatchFolder.mkdir(); | |
} | |
for (int i = 0; i < NewFiles.size(); i++) { | |
try { | |
String fileName = NewVersionOutputFolder + NewFiles.get(i); | |
String patchfilename = PatchFolderDir + NewFiles.get(i); | |
////Modification | |
// patchfilename = PatchFolderDir + NewFiles.get(i)+".bz2"; | |
/////// | |
fileName = fileName.replace("/", "\\"); | |
patchfilename = patchfilename.replace("/", "\\"); | |
File copyfile = new File(fileName); | |
////Modification | |
// copyfile=Bzip2file(copyfile); | |
/////// | |
File patchfile = new File(patchfilename); | |
String patchfilefolder = ""; | |
int l = patchfilename.lastIndexOf("\\"); | |
patchfilefolder = patchfilename.substring(0, l); | |
// System.out.println("patch file name :"+difffilename); | |
// System.out.println("pathfilefolder :"+patchfilefolder); | |
File Folder = new File(patchfilefolder); | |
if (!Folder.exists()) { | |
Folder.mkdirs(); | |
} | |
if (!patchfile.exists()) { | |
// patchfile.setWritable(true); | |
if (patchfile.createNewFile()) { //System.out.println("file created successfully!!"); | |
} else { | |
System.out.println("failed!!!!!!!1"); | |
} | |
} | |
InputStream inStream = null; | |
OutputStream outStream = null; | |
inStream = new FileInputStream(copyfile); | |
outStream = new FileOutputStream(patchfile); | |
byte[] buffer = new byte[inStream.available()]; | |
int length; | |
//copy the file content in bytes | |
while ((length = inStream.read(buffer)) > 0) { | |
outStream.write(buffer, 0, length); | |
} | |
inStream.close(); | |
outStream.close(); | |
//delete the original file | |
// System.out.println("File is copied successful!"); | |
} catch (Exception ex) { | |
Logger.getLogger("Exception file create>>>" + ex); | |
} | |
} | |
System.out.println("Updated files dif copying..."); | |
String oldfileerr = ""; | |
String newfileerr = ""; | |
for (int i = 0; i < UpdatedFiles.size(); i++) { | |
// System.out.println("up>"+UpdatedFiles.get(i)); | |
} | |
for (int i = 0; i < UpdatedFiles.size(); i++) { | |
try { | |
if (UpdatedFiles.get(i).contains("list_item_headline_first")) { | |
System.out.println("" + UpdatedFiles.get(i)); | |
System.out.println(":("); | |
} | |
String newfileName = NewVersionOutputFolder + UpdatedFiles.get(i); | |
String oldfileName = OldVersionOutputFolder + UpdatedFiles.get(i); | |
String difffilename = PatchFolderDir + UpdatedFiles.get(i); | |
newfileName = newfileName.replace("/", "\\"); | |
oldfileName = oldfileName.replace("/", "\\"); | |
difffilename = difffilename.replace("/", "\\"); | |
oldfileerr = oldfileName; | |
newfileerr = newfileName; | |
//File patchfile = new File(difffilename); | |
String patchfilefolder = ""; | |
int l = difffilename.lastIndexOf("\\"); | |
patchfilefolder = difffilename.substring(0, l); | |
// System.out.println("oldfilename :"+oldfileName); | |
// System.out.println("new File name :"+newfileName); | |
// System.out.println("patch file name :"+difffilename); | |
// System.out.println("pathfilefolder :"+patchfilefolder); | |
File Folder = new File(patchfilefolder); | |
if (!Folder.exists()) { | |
Folder.mkdirs(); | |
} | |
File oldFile = new File(oldfileName); | |
File newFile = new File(newfileName); | |
File diffFile = new File(difffilename); | |
new JBDiff().bsdiff(oldFile, newFile, diffFile); | |
///Modification | |
// diffFile =Bzip2file(diffFile); | |
///// | |
// System.out.println("\ndiffile size:"+diffFile.length()); | |
// System.out.println("newfile size:"+newFile.length()); | |
if (diffFile.length() > newFile.length()) { | |
System.out.println("\ndiffile size:" + diffFile.length()); | |
System.out.println("newfile size:" + newFile.length()); | |
System.out.println("patch file name :" + difffilename); | |
System.out.println("difffile larger"); | |
diffFile.delete(); | |
if (!diffFile.exists()) { | |
// patchfile.setWritable(true); | |
if (diffFile.createNewFile()) { //System.out.println("file created successfully!!"); | |
} else { | |
System.out.println("failed!!!!!!!1"); | |
} | |
} | |
String remarkedfile = UpdatedFiles.get(i); | |
NewFiles.add(UpdatedFiles.get(i)); | |
DeletedFiles.add(UpdatedFiles.get(i)); | |
UpdatedFiles.remove(remarkedfile); | |
i--; | |
InputStream inStream = null; | |
OutputStream outStream = null; | |
inStream = new FileInputStream(newFile); | |
outStream = new FileOutputStream(diffFile); | |
byte[] buffer = new byte[inStream.available()]; | |
int length; | |
//copy the file content in bytes | |
while ((length = inStream.read(buffer)) > 0) { | |
outStream.write(buffer, 0, length); | |
} | |
inStream.close(); | |
outStream.close(); | |
} | |
} catch (Exception ex) { | |
System.err.println("Error >>>" + ex); | |
System.out.println("old file:" + oldfileerr); | |
System.out.println("old file:" + newfileerr); | |
} | |
} | |
try { | |
copyFolder(new File(NewVersionManifestFolder), new File(PatchFolderDir + "META-INF\\")); | |
} catch (IOException ex) { | |
// Logger.getLogger(StartForm.class.getName()).log(Level.SEVERE, null, ex); | |
} | |
// JOptionPane.showMessageDialog(this, "Patch Folder Created Successfully !!!", "Infomation", TrayIcon.MessageType.INFO.ordinal()); | |
// jLabel8.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/ok.png"))); | |
} | |
if (true) | |
{ | |
if (new File(PatchFolderDir).exists()) { | |
File PatchManifest = new File(PatchFolderDir + "\\PatchManifest.xml"); | |
PatchManifest.createNewFile(); | |
FileWriter fw = new FileWriter(PatchManifest.getAbsoluteFile()); | |
BufferedWriter bw = new BufferedWriter(fw); | |
String OldVersion = pakageversion; | |
String NewVersion = db.getNewApkVersion(pakagename); | |
bw.write("OldVersion: " + OldVersion + "\n"); | |
bw.write("NewVersion: " + NewVersion + "\n"); | |
bw.write("DeletedFiles:\n"); | |
for (int i = 0; i < DeletedFiles.size(); i++) { | |
bw.write(DeletedFiles.get(i).trim() + "\n"); | |
} | |
bw.write("NewFiles:\n"); | |
for (int i = 0; i < NewFiles.size(); i++) { | |
bw.write(NewFiles.get(i).trim() + "\n"); | |
} | |
bw.write("UpdatedFiles:\n"); | |
for (int i = 0; i < UpdatedFiles.size(); i++) { | |
bw.write(UpdatedFiles.get(i).trim() + "\n"); | |
} | |
bw.close(); | |
String patchfolder = PatchFolderDir.substring(0, PatchFolderDir.length() - 1); | |
//////////////////convert folder to a zip folder///////////////// | |
// //create a ZipOutputStream to zip the data to | |
// String Folder=patchfolder.substring(patchfolder.lastIndexOf("\\"),patchfolder.length()); | |
// ZipOutputStream zos = new | |
// ZipOutputStream(new FileOutputStream(patchfolder+".zip")); | |
// //assuming that there is a directory named inFolder (If there | |
// //isn't create one) in the same directory as the one the code | |
// // runs from, | |
// //call the zipDir method | |
// zipDir(patchfolder, zos); | |
// //close the stream | |
// zos.close(); | |
///////////////second type of zip compresion | |
FileOutputStream fos = new FileOutputStream(patchfolder + ".zip"); | |
ZipOutputStream zos = new ZipOutputStream(fos); | |
//level - the compression level (0-9) | |
zos.setLevel(9); | |
System.out.println("Begin to compress folder : " + patchfolder + " to " + patchfolder + ".zip"); | |
addFolder(zos, patchfolder, patchfolder); | |
zos.close(); | |
/////////////////////////////////////////////////////////////////// | |
File patchFolder = new File(patchfolder + ".zip"); | |
if (!patchFolder.exists()) { | |
patchFolder.mkdir(); | |
} | |
patchFolder.setReadable(true); | |
File patchFilebzip2 = new File(patchfolder + ".bz2"); | |
if (!patchFilebzip2.exists()) { | |
patchFilebzip2.createNewFile(); | |
patchFilebzip2.setWritable(true); | |
} | |
System.out.println("PatchFile Path : "+patchFilebzip2.getAbsolutePath()); | |
patchfilepath=patchFilebzip2.getAbsolutePath(); | |
patchfilepath=patchfilepath.substring(patchfilepath.lastIndexOf("\\")+1,patchfilepath.length()); | |
System.out.println("filepath::>>>> "+patchfilepath); | |
InputStream fileInputStream = new BufferedInputStream(new FileInputStream(patchFolder)); | |
OutputStream fileOutputStream = new BufferedOutputStream(new FileOutputStream(patchFilebzip2)); | |
BZip2OutputStream outputStream = new BZip2OutputStream(fileOutputStream); | |
byte[] buffer = new byte[fileInputStream.available()]; | |
int bytesRead; | |
while ((bytesRead = fileInputStream.read(buffer)) != -1) { | |
outputStream.write(buffer, 0, bytesRead); | |
} | |
outputStream.close(); | |
fileInputStream.close(); | |
fileOutputStream.close(); | |
// //////decompress .bz2 to zip | |
// File newf = new File(patchfolder + "decm.zip"); | |
// InputStream fileInputStream1 = new BufferedInputStream(new FileInputStream(patchFilebzip2)); | |
// BZip2InputStream inputStream = new BZip2InputStream(fileInputStream1, false); | |
// OutputStream fileOutputStream1 = new BufferedOutputStream(new FileOutputStream(newf), fileInputStream1.available()); | |
// | |
// byte[] decoded = new byte[fileInputStream1.available()]; | |
// int bytesRead1; | |
// while ((bytesRead1 = inputStream.read(decoded)) != -1) { | |
// fileOutputStream1.write(decoded, 0, bytesRead1); | |
// } | |
// fileOutputStream1.close(); | |
// inputStream.close(); | |
// fileInputStream1.close(); | |
// ////////////// | |
// | |
// /////////////unzipng zipfile | |
// | |
// unZipIt(newf); | |
// //////////////////// | |
////////////////Delete temp files and folders////////////// | |
File oldversionfolderdelete=new File(OldVersionOutputFolder.substring(0, OldVersionOutputFolder.length()-1)); | |
if(oldversionfolderdelete.exists()) | |
{ | |
delete(oldversionfolderdelete); | |
} | |
File newversionfolderdelete=new File(NewVersionOutputFolder.substring(0, NewVersionOutputFolder.length()-1)); | |
if(newversionfolderdelete.exists()) | |
{ | |
delete(newversionfolderdelete); | |
} | |
File patchfolderdelete=new File(PatchFolderDir.substring(0, PatchFolderDir.length()-1)); | |
if(patchfolderdelete.exists()) | |
{ | |
delete(patchfolderdelete); | |
} | |
if(patchFolder.exists()) | |
patchFolder.delete(); | |
// JOptionPane.showMessageDialog(this,"Delta++ File Created successfully !!!","Infomation", TrayIcon.MessageType.INFO.ordinal()); | |
// jLabel9.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/ok.png"))); | |
} else { | |
// JOptionPane.showInternalMessageDialog(this, "Patch Folder Not Exits", "No Patch Folder Found", TrayIcon.MessageType.WARNING.ordinal()); | |
} | |
} else { | |
// JOptionPane.showMessageDialog(this, "Specify Version to create PatchManifest.xml", "Version Name Not Found", TrayIcon.MessageType.WARNING.ordinal()); | |
} | |
//////////// | |
} | |
} | |
// getServletContext().getInitParameter("file-upload" | |
// out.println("http://10.0.3.2:8084/Delta___WebServer/UploadFiles/"+patchfilepath); | |
String baseUrl = | |
request.getScheme() + "://" + | |
request.getServerName() + ":" + request.getServerPort() + | |
request.getContextPath(); | |
out.println(baseUrl+"/UploadFiles/"+patchfilepath); | |
// OutputStream output = response.getOutputStream(); | |
// FileInputStream in = new FileInputStream(new File("D:\\Jitty\\Android workspace netbeans\\Delta++ WebServer\\web\\UploadFiles\\soft.kinoko.SilentCamera3.1.7.apk")); | |
// byte[] buffer = new byte[4096]; | |
// int length; | |
// while ((length = in.read(buffer)) > 0) { | |
// output.write(buffer, 0, length); | |
// } | |
// in.close(); | |
// output.flush(); | |
// String Name = (String) request.getParameter("name"); | |
// String Version = (String) request.getParameter("version"); | |
/* TODO output your page here. You may use following sample code. */ | |
// out.println("<!DOCTYPE html>"); | |
// out.println("<html>"); | |
// out.println("<head>"); | |
// out.println("<title>Servlet Getnewapk</title>"); | |
// out.println("</head>"); | |
// out.println("<body>"); | |
// out.println("<h1>Servlet Getnewapk at " + request.getContextPath() + "</h1>"); | |
// out.println("</body>"); | |
// out.println("</html>"); | |
} finally { | |
out.close(); | |
} | |
} | |
// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code."> | |
/** | |
* Handles the HTTP | |
* <code>GET</code> method. | |
* | |
* @param request servlet request | |
* @param response servlet response | |
* @throws ServletException if a servlet-specific error occurs | |
* @throws IOException if an I/O error occurs | |
*/ | |
@Override | |
protected void doGet(HttpServletRequest request, HttpServletResponse response) | |
throws ServletException, IOException { | |
processRequest(request, response); | |
} | |
/** | |
* Handles the HTTP | |
* <code>POST</code> method. | |
* | |
* @param request servlet request | |
* @param response servlet response | |
* @throws ServletException if a servlet-specific error occurs | |
* @throws IOException if an I/O error occurs | |
*/ | |
@Override | |
protected void doPost(HttpServletRequest request, HttpServletResponse response) | |
throws ServletException, IOException { | |
processRequest(request, response); | |
} | |
/** | |
* Returns a short description of the servlet. | |
* | |
* @return a String containing servlet description | |
*/ | |
@Override | |
public String getServletInfo() { | |
return "Short description"; | |
}// </editor-fold> | |
private String getFileNameWithOutExtn(String FileName) { | |
int l = FileName.lastIndexOf("."); | |
FileName = FileName.substring(0, l); | |
return FileName; | |
} | |
public void unZipIt(File zipfile) { | |
String zipFile = zipfile.getAbsolutePath(); | |
String filename = getFileNameWithOutExtn(zipfile.getName()); | |
String outputFolder = zipfile.getParent() + "\\" + filename + "\\"; | |
byte[] buffer = new byte[1024]; | |
try { | |
//create output directory is not exists | |
File folder = new File(outputFolder); | |
if (!folder.exists()) { | |
folder.mkdir(); | |
} | |
//get the zip file content | |
ZipInputStream zis = | |
new ZipInputStream(new FileInputStream(zipFile)); | |
//get the zipped file list entry | |
ZipEntry ze = zis.getNextEntry(); | |
while (ze != null) { | |
String fileName = ze.getName(); | |
File newFile = new File(outputFolder + File.separator + fileName); | |
//System.out.println("file unzip : "+ newFile.getAbsoluteFile()); | |
//create all non exists folders | |
//else you will hit FileNotFoundException for compressed folder | |
new File(newFile.getParent()).mkdirs(); | |
FileOutputStream fos = new FileOutputStream(newFile); | |
int len; | |
while ((len = zis.read(buffer)) > 0) { | |
fos.write(buffer, 0, len); | |
} | |
fos.close(); | |
ze = zis.getNextEntry(); | |
} | |
zis.closeEntry(); | |
zis.close(); | |
System.out.println("Done"); | |
} catch (Exception ex) { | |
ex.printStackTrace(); | |
} | |
} | |
public static void copyFolder(File src, File dest) | |
throws IOException { | |
if (src.isDirectory()) { | |
//if directory not exists, create it | |
if (!dest.exists()) { | |
dest.mkdir(); | |
System.out.println("Directory copied from " | |
+ src + " to " + dest); | |
} | |
//list all the directory contents | |
String files[] = src.list(); | |
for (String file : files) { | |
//construct the src and dest file structure | |
File srcFile = new File(src, file); | |
File destFile = new File(dest, file); | |
//recursive copy | |
copyFolder(srcFile, destFile); | |
} | |
} else { | |
//if file, then copy it | |
//Use bytes stream to support all file types | |
InputStream in = new FileInputStream(src); | |
OutputStream out = new FileOutputStream(dest); | |
byte[] buffer = new byte[1024]; | |
int length; | |
//copy the file content in bytes | |
while ((length = in.read(buffer)) > 0) { | |
out.write(buffer, 0, length); | |
} | |
in.close(); | |
out.close(); | |
System.out.println("File copied from " + src + " to " + dest); | |
} | |
} | |
private static void addFolder(ZipOutputStream zos, String folderName, String baseFolderName) { | |
try{ | |
File f = new File(folderName); | |
if (f.exists()) { | |
if (f.isDirectory()) { | |
File f2[] = f.listFiles(); | |
for (int i = 0; i < f2.length; i++) { | |
addFolder(zos, f2[i].getAbsolutePath(), baseFolderName); | |
} | |
} else { | |
//add file | |
//extract the relative name for entry purpose | |
String entryName = folderName.substring(baseFolderName.length() + 0, folderName.length()); | |
//System.out.print("Adding entry " + entryName + "..."); | |
ZipEntry ze = new ZipEntry(entryName); | |
zos.putNextEntry(ze); | |
FileInputStream in = new FileInputStream(folderName); | |
int len; | |
byte buffer[] = new byte[1024]; | |
while ((len = in.read(buffer)) > 0) { | |
zos.write(buffer, 0, len); | |
} | |
in.close(); | |
zos.closeEntry(); | |
// System.out.println("OK!"); | |
} | |
} else { | |
System.out.println("File or directory not found " + folderName); | |
} | |
} | |
catch(Exception e) | |
{ | |
e.printStackTrace(); | |
} | |
} | |
public static void delete(File file) | |
{ | |
try{ | |
if(file.isDirectory()){ | |
//directory is empty, then delete it | |
if(file.list().length==0){ | |
file.delete(); | |
// System.out.println("Directory is deleted : " | |
// + file.getAbsolutePath()); | |
}else{ | |
//list all the directory contents | |
String files[] = file.list(); | |
for (String temp : files) { | |
//construct the file structure | |
File fileDelete = new File(file, temp); | |
//recursive delete | |
delete(fileDelete); | |
} | |
//check the directory again, if empty then delete it | |
if(file.list().length==0){ | |
file.delete(); | |
// System.out.println("Directory is deleted : " | |
// + file.getAbsolutePath()); | |
} | |
} | |
}else{ | |
//if file, then delete it | |
file.delete(); | |
//System.out.println("File is deleted : " + file.getAbsolutePath()); | |
} | |
} | |
catch(Exception e) | |
{ | |
e.printStackTrace(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment