Fri Apr 06, 2007 10:20 am
<%@ page import="java.io.*" %>
<%@ page import="org.apache.tools.zip.ZipEntry" %>
<%@ page import="org.apache.tools.zip.ZipOutputStream" %>
<%
/* By Sami and El-dib */
String filename = "";
ServletInputStream in = request.getInputStream();
byte[] line = new byte[128*2];
long file_size = 0;
int i = in.readLine(line, 0, 128*2);
int boundaryLength = i - 2;
String boundary = new String(line, 0, boundaryLength); //-2
while (i != -1) {
String newLine = new String(line, 0, i);
if (newLine.startsWith("Content-Disposition: form-data; name=\""))
{
String s = new String(line, 0, i-2);
int pos = s.indexOf("filename=\"");
if (pos != -1) {
String filepath = s.substring(pos+10, s.length()-1);
// Windows browsers include the full path on the client
// But Linux/Unix and Mac browsers only send the filename
// test if this is from a Windows browser
pos = filepath.lastIndexOf("\\");
if (pos != -1)
filename = filepath.substring(pos + 1);
else
filename = filepath;
}
//this is the file content
i = in.readLine(line, 0, 128*2);
i = in.readLine(line, 0, 128*2);
// blank line
i = in.readLine(line, 0, 128*2);
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
newLine = new String(line, 0, i);
while (i != -1 && !newLine.startsWith(boundary)) {
// the problem is the last line of the file content
// contains the new line character.
// So, we need to check if the current line is
// the last line.
buffer.write(line, 0, i);
i = in.readLine(line, 0, 128*2);
newLine = new String(line, 0, i);
}
// save the uploaded file
byte[] bytes = buffer.toByteArray();
// Create a buffer for reading the files
String[] filenames = new String[]{filename};
// Create a buffer for reading the files
// Compress the files
// Add ZIP entry to output stream.
try
{
File myfile = new File("/example/1/");
myfile.mkdir();
// Transfer bytes from the file to the ZIP file
// ZipOutputStream outz = new ZipOutputStream(new FileOutputStream("home/content/M/o/h/Mohamed2007/html/1/1_subarabia.zip"));
ZipOutputStream outz = new ZipOutputStream(new FileOutputStream("/example/1/"));
outz.putNextEntry(new ZipEntry(filename));
outz.write( bytes,0, bytes.length-2);
outz.closeEntry();
outz.close();
} catch (IOException ex) {
out.println(ex.toString());
}
}
i = in.readLine(line, 0, 128*2);
}
out.println("Finish");
%>
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com
Powered by phpBB © phpBB Group.