convert a file .doc to .zip

Ads
 

convert a file .doc to .zip

hiii, I uploaded a file with extension .doc now i required with .zip

View Answers

January 4, 2011 at 3:55 PM

Hi Friend,

Try the following code:

1)page.jsp:

<%@ page language="java" %>
<HTml>
<HEAD><TITLE>Display file upload form to the user</TITLE></HEAD>

<BODY> <FORM ENCTYPE="multipart/form-data" ACTION=
"uploadandstore.jsp" METHOD=POST>
<br><br><br>
<center>
<table border="0" bgcolor=#ccFDDEE>
<tr>
<center>
<td colspan="2" align="center"><B>UPLOAD THE FILE</B><center></td>
</tr>
<tr>
<td colspan="2" align="center"> </td>
</tr>
<tr>
<td><b>Choose the file To Upload:</b></td>
<td><INPUT NAME="file" TYPE="file"></td>
</tr>
<tr>
<td colspan="2" align="center"> </td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" value="Send File"> </td>
</tr>
<table>
</center>
</FORM>
</BODY>
</HTML>

2)uploadandstore.jsp:

<%@ page import="java.io.*" %>
<%@ page import="java.sql.*" %>
<%@ page import="java.util.zip.*"%>
<%
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="C:/"+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><b>You have successfully upload the file:</b>
<% out.println(saveFile);%></td></tr></table>
<%

    String filesToZip = saveFile;
    File f = new File(filesToZip);
  String zipFileName="";
  int index = f.getName().lastIndexOf('.');
      if (index>0&& index <= f.getName().length() - 2 ) {
      zipFileName=f.getName().substring(0, index);
      }
  System.out.println(zipFileName);
    zipFileName = zipFileName + ".zip";

    byte[] buffer = new byte[18024];
    try{
      ZipOutputStream zos = new ZipOutputStream(new FileOutputStream("C:/"+zipFileName));
      zos.setLevel(Deflater.DEFAULT_COMPRESSION);
      FileInputStream fis = new FileInputStream(filesToZip);
      zos.putNextEntry(new ZipEntry(filesToZip));
      int len;
      while ((len = fis.read(buffer)) > 0){
        zos.write(buffer, 0, len);
      }
      zos.closeEntry();
      fis.close();
      zos.close();
    }
    catch(Exception e){
        System.out.println(e);
    }
}

  %>

Thanks


January 4, 2011 at 3:55 PM

Hi Friend,

Try the following code:

1)page.jsp:

<%@ page language="java" %>
<HTml>
<HEAD><TITLE>Display file upload form to the user</TITLE></HEAD>

<BODY> <FORM ENCTYPE="multipart/form-data" ACTION=
"uploadandstore.jsp" METHOD=POST>
<br><br><br>
<center>
<table border="0" bgcolor=#ccFDDEE>
<tr>
<center>
<td colspan="2" align="center"><B>UPLOAD THE FILE</B><center></td>
</tr>
<tr>
<td colspan="2" align="center"> </td>
</tr>
<tr>
<td><b>Choose the file To Upload:</b></td>
<td><INPUT NAME="file" TYPE="file"></td>
</tr>
<tr>
<td colspan="2" align="center"> </td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" value="Send File"> </td>
</tr>
<table>
</center>
</FORM>
</BODY>
</HTML>

2)uploadandstore.jsp:

<%@ page import="java.io.*" %>
<%@ page import="java.sql.*" %>
<%@ page import="java.util.zip.*"%>
<%
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="C:/"+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><b>You have successfully upload the file:</b>
<% out.println(saveFile);%></td></tr></table>
<%

    String filesToZip = saveFile;
    File f = new File(filesToZip);
  String zipFileName="";
  int index = f.getName().lastIndexOf('.');
      if (index>0&& index <= f.getName().length() - 2 ) {
      zipFileName=f.getName().substring(0, index);
      }
  System.out.println(zipFileName);
    zipFileName = zipFileName + ".zip";

    byte[] buffer = new byte[18024];
    try{
      ZipOutputStream zos = new ZipOutputStream(new FileOutputStream("C:/"+zipFileName));
      zos.setLevel(Deflater.DEFAULT_COMPRESSION);
      FileInputStream fis = new FileInputStream(filesToZip);
      zos.putNextEntry(new ZipEntry(filesToZip));
      int len;
      while ((len = fis.read(buffer)) > 0){
        zos.write(buffer, 0, len);
      }
      zos.closeEntry();
      fis.close();
      zos.close();
    }
    catch(Exception e){
        System.out.println(e);
    }
}

  %>

Thanks

Ads









Related Tutorials/Questions & Answers:
convert a file .doc to .zip
convert a file .doc to .zip  hiii, I uploaded a file with extension .doc now i required with .zip   Hi Friend, Try the following code...;TITLE>Display file upload form to the user</TITLE></HEAD> <
convert zip file to byte array
convert zip file to byte array  Hi,How to convert zip file to byte array,can you please provide program?? Thanks, Ramanuja
Advertisements
how to convert doc file into html using java swing
how to convert doc file into html using java swing  i want to convert doc file into html and display it on jtextarea please help me and give the sample code
Convesion of txt file to doc file.??????
Convesion of txt file to doc file.??????  how to convert text file to doc file using java
sending a zip file to servlet
sending a zip file to servlet  I have created a .zip file in a servlet on the local system(no static ip). The .zip file contains xml files. I have to send it to another servlet which is in a server(has a static ip). I have done
How to zip a file using javascrip?
How to zip a file using javascrip?  hi.. can anyone tell me How to zip a file using javascrip
How to convert multiple files in java to .zip format
How to convert multiple files in java to .zip format  i receive...); } i want to convert the data from inputStream into Zip Stream and so...(); } out.close(); fout.close(); System.out.println("Zip File is created
doc file - Java Beginners
doc file   i want to read .doc file ?....it is a part of my project plz help. if possible , can anyone give me the code
Read file zip in J2ME - MobileApplications
Read file zip in J2ME  I would like to read file.zip in J2ME,I don't know how to extract it. Please help me
Write data in doc file
Write data in doc file  How to write data or string in word file
read doc and docx file in javascript
read doc and docx file in javascript  How i read doc and docx file in javascript
Issue in Converting .zip file to .jar file?
Issue in Converting .zip file to .jar file?  I have a folder called "printpdf.jar". I have changed the .jar to .zip and did some customizations. After which i had "zipped" it again and made as ".jar". Now the final file
Reading .doc file using swing
Reading .doc file using swing  Sir, Could U Please Tell me the Way To Read .doc file in java using swing,with code
Java code to convert pdf file to word file
Java code to convert pdf file to word file  How to convert pdf file to word file using Java
how to convert .xml file to csv file
how to convert .xml file to csv file  how to convert .xml file to csv file
Convert Text File to PDF file
Convert Text File to PDF file  Here is the way how to covert your Text file to PDF File, public class TextFileToPDF { private static void... BufferedReader(new FileReader( "**INPUT FILE**")); String inLine
How to convert XML file to database ?
How to convert XML file to database ?  How to convert XML file to database
Where to download spring framework zip file?
Where to download spring framework zip file?  Hi, I am trying to find the link to download the full distribution zip file of the Spring Framework.... Can anyone tell me the url from where I can download the full zip file
how to convert a jar file into .exe file
how to convert a jar file into .exe file  hi, I want convert my jar file into executable file,urgently please help me
Convert Text File to PDF file
Convert Text File to PDF file  import java.io.BufferedReader; import... FileReader( Input File)); String inLine = null... FileOutputStream( output file)); document.open(); document.add
How to create a zip file and how to compare zip size with normal text file
How to create a zip file and how to compare zip size with normal text file .../ZipCreateExample.shtml but when i verified the size of zip and normal file,normal file is showing as 12 bytes and zip file was showing 150bytes in Dos prompt.Is there any error
how to convert image file to text file?
how to convert image file to text file?  hi, can anybody tell how to convert image file to text file? plz help me
find zip file length lessthen 5MB
find zip file length lessthen 5MB  import java.io.BufferedReader...(); } else{ System.out.println("zip file...; } } System.out.println("Zip File
Java convert file to byte array
Java convert file to byte array  How to convert file to byte array in Java? Thanks   Hi, To convert file to byte array in Java you have.... Read the complete examples at: Reading file into bytearrayoutputstream Reading
convert an pdf file to html in Java
convert an pdf file to html in Java  Hi all, How to convert an pdf file to html in Java? Currently all my data is generated into a report in pdf and i want to be able to generate it to html page as well. How to go about
serch document from .doc and .docx file
serch document from .doc and .docx file  how to find particular word from .doc and .docx file and that date insert into a data base
openning the pdf or doc file in same jsp
openning the pdf or doc file in same jsp   **how to open the pdf and doc file in same jsp after clicking the link or button (which is with out moving to the next page). will some body help me on this @simple format please
How to upload zip file from android to server programmatically?????
How to upload zip file from android to server programmatically?????  hi , I want to upload zip file from android phone sdcard to server programmatically. So any one do this task in past so please share your experience with me
How to read the .doc/ .docx file in Java Program
How to read the .doc/ .docx file in Java Program  Hi, I am beginner in Java programming language. Can anybody explain How to read .doc file in Java.... This way you can read the .doc or .docx file in Java programming language. Thanks
CONVERT JTable DATA TO PDF FILE
CONVERT JTable DATA TO PDF FILE  HOW TO CONVERT JTable DATA TO .PDF FILE??PLEASE GIVE ME CODE FOR THAT.   Here is an example that reads the jtable data from the jframe and stored the data into the pdf file in the form
how to convert war file into .exe file using java code
how to convert war file into .exe file using java code  hi,I am beginner in the java,I want to convert my java maven project to .exe file,plz tell me which is required jar files and how i convert this..?Thanks in advance

Ads