ExtractFileSubDirectories.java
-------------------------------
public class ExtractFileSubDirectories {
public static void unzip(String strZipFile) {
try
{
System.out.println("In java Class:");
File fSourceZip = new File(strZipFile);
String zipPath = strZipFile.substring(0, strZipFile.length()-4);
File temp = new File(zipPath);
temp.mkdir();
System.out.println(zipPath + " created");
ZipFile zipFile = new ZipFile(fSourceZip);
Enumeration e = zipFile.entries();
while(e.hasMoreElements())
{
ZipEntry entry = (ZipEntry)e.nextElement();
File destinationFilePath = new File(zipPath,entry.getName());
destinationFilePath.getParentFile().mkdirs();
if(entry.isDirectory())
{
continue;
}
else
{
System.out.println("Extracting " + destinationFilePath);
BufferedInputStream bis = new BufferedInputStream(zipFile.getInputStream(entry));
int b;
byte buffer[] = new byte[1024];
FileOutputStream fos = new FileOutputStream(destinationFilePath);
BufferedOutputStream bos = new BufferedOutputStream(fos,
1024);
while ((b = bis.read(buffer, 0, 1024)) != -1) {
bos.write(buffer, 0, b);
}
bos.flush();
bos.close();
fos.close();
bis.close();
}
}
zipFile.close();
System.out.println(fSourceZip.delete());
}
catch(IOException ioe)
{
ioe.printStackTrace();
}
catch(Exception e){
e.printStackTrace();
}
}
}
jsp page:
Fileupload.jsp
<![CDATA[<%
String OUTPUT_FOLDER = getServletContext().getRealPath("/Upload");
String saveFile = "";
String contentType = request.getContentType();
if ((contentType != null) && (contentType.indexOf("multipart/form-data") >= 0)) {
DataInputStream in = new DataInputStream(request.getInputStream());
int formDataLength = request.getContentLength();
byte dataBytes[] = new byte[formDataLength];
int byteRead = 0;
int totalBytesRead = 0;
while (totalBytesRead < formDataLength) {
byteRead = in.read(dataBytes, totalBytesRead, formDataLength);
totalBytesRead += byteRead;
}
String file = new String(dataBytes);
saveFile = file.substring(file.indexOf("filename=\"") + 10);
saveFile = saveFile.substring(0, saveFile.indexOf("\n"));
saveFile = saveFile.substring(saveFile.lastIndexOf("\\") + 1, saveFile.indexOf("\""));
int lastIndex = contentType.lastIndexOf("=");
String boundary = contentType.substring(lastIndex + 1, contentType.length());
int pos;
pos = file.indexOf("filename=\"");
pos = file.indexOf("\n", pos) + 1;
pos = file.indexOf("\n", pos) + 1;
pos = file.indexOf("\n", pos) + 1;
int boundaryLocation = file.indexOf(boundary, pos) - 4;
int startPos = ((file.substring(0, pos)).getBytes()).length;
int endPos = ((file.substring(0, boundaryLocation)).getBytes()).length;
saveFile = OUTPUT_FOLDER +"\\"+ saveFile;
File ff = new File(saveFile);
FileOutputStream fileOut = new FileOutputStream(ff);
fileOut.write(dataBytes, startPos, (endPos - startPos));
fileOut.flush();
fileOut.close();
%>]]><br>
<table border="2">
<tr>
<td>You have successfully upload the file by the name of:
<%
out.println(saveFile);
com.winnova.struts.action.ExtractFileSubDirectories.unzip(saveFile);
}
else{
System.out.println("------------------"+contentType);
}
%>
</td>
</tr>
</table>
</br>
<HTML>
<HEAD>
<TITLE>Display file upload form to the user</TITLE>
</HEAD>
<BODY>
<FORM ENCTYPE="multipart/form-data" ACTION="fileUpload.jsp" METHOD=POST>
<br>
<br>
<br>
<table border="1" bgcolor="#ccFDDEE">
<tr>
<td colspan="2" align="center">UPLOAD THE FILE</td>
</tr>
<tr>
<td>File Name:</td>
<td><INPUT NAME="files" TYPE="text"></td>
</tr>
<tr>
<td>Choose the file To Upload:</td>
<td><INPUT NAME="file" TYPE="file"></td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" value="Send File"></td>
</tr>
</table>
</FORM>
</BODY>
</HTML>
</br></br></br>