Follow the steps given below to Unzip a file on Server:
1. Create a File object from the uplodaded data - If you have used Struts framwork to upload a Zip file, then first get the FormFile object (org.apache.struts.upload.FormFile).
FormFile formFile = form.getFileProperties();
From FormFile object get the InputStream.
InputStream inputStream = formFile.getInputStream();
In case we are getting the data within a hidden field in base-64 / zipped format, FormFile is null. In that case get the byte[] data using: sun.misc.BASE64Decoder().decodeBuffer() method if Enctype is *B64*
if(form.getEnctype().equals("*B64*"))
byte[] rawData = sun.misc.BASE64Decoder().decodeBuffer(form.Data());
Create ByteArryaIntputStream from byte[] data.
InputStream inputStream = new ByteArrayInputStream(rawData);
Now create a File object in which you want to read the InputStream.
File fileObj = new File("test.zip");
Create a FileOutputStream from the File object.
OutputStream outputStream = new FileOutputStream(fileObj);
Now read the IntputStream and write it to OutputStream.
2. Once the File object is created, next is to create a ZipFile (java.util.zip.ZipFile) object from it.
ZipFile zipFile = new ZipFile(fileObject);
3. Iterate through the ZipFile to read the zip and extract its contents.
Below is the complete soure code.
import org.apache.struts.upload.*;
import java.util.*;
import java.util.zip.ZipFile;
import java.util.zip.ZipEntry;
import java.io.*;
public class unzipUtility
{
public static final String B64String = "*B64*";
public void unzipFile(ActionForm form)
{
InputStream inputStream = null;
OutputStream outputStream = null;
File fileObj = new File("test.zip");
FormFile formFile = form.getFormFile();// not the standard method
if(formFile!=null)
{
inputStream = formFile.getInputStream();
}
else
{
byte[] rawData = null;
if(form.getEnctype().equals(B64String))
{
rawData = new sun.misc.BASE64Decoder().decodeBuffer(form.getData());
}
inputStream = new ByteArrayInputStream(rawData);
}
outputStream = FileOutputStream(fileObj);
copyStream(inputStream, outputStream);
}
private byte[] getRawData(String content)
{
}// getRawData()
private void copyStream(InputStream input, OutputStream output)
{
int size;
byte[] buffer = new byte[1024];
while((size=input.read(buffer))>=0)
{
output.write(buffer, 0, size);
}//while
input.close();
output.close();
}//copyStream()
public void unzip(File fileObj)
{
Enumeration entries;
ZipFile zipFile;
try {
zipFile = new ZipFile(fileObj);
entries = zipFile.entries();
while(entries.hasMoreElements()) {
ZipEntry entry = (ZipEntry)entries.nextElement();
if(entry.isDirectory()) {
// Assume directories are stored parents first then children.
System.err.println("Extracting directory: " + entry.getName());
// This is not robust, just for demonstration purposes.
(new File(entry.getName())).mkdir();
continue;
}
System.err.println("Extracting file: " + entry.getName());
copyInputStream(zipFile.getInputStream(entry),
new BufferedOutputStream(new FileOutputStream(entry.getName())));
}
zipFile.close();
} catch (IOException ioe) {
System.err.println("Unhandled exception:");
ioe.printStackTrace();
return;
}
}
}
Better Tools Happy Engineers
-
*"The only thing constant in a Startup is Change"*
If you aren't changing fast enough then order and complacency sets in which
leads to mediocrity and you...
7 years ago
No comments:
Post a Comment