Applies To: Jdk 1.5 on wards
Requirement:
In many applications, we might require to zip a runtime content and store them in hard disk level.
Author(s): Biplab Ray
Company: Tata Consultancy Services
Created on: 07th March 2015
Author Bio:
Biplab Ray is working for Tata Consultancy Services as Assistant Consultant and development of SAP EP, composite applications using CAF, BPM, BRM, WebDynpro for Java. He also works in technologies like Java/J2EE and has depth understanding on eSOA, Webservice, Enterprise Service, XML.
Implementation:
In the example I have taken a XML string as run time input of the application.
Steps of the application:
First: Make a file in Hard disk with the content which is provided during the run time with help of the method: writeToHD, the method would return an absolute path of the file.
Second: Make zip file with help of the method: createZip and the method would return an absolute path of the zip file.
Third: We have to delete the file other than zip, with help of the method: deleteFile
Fourth: Now we have to read the zip file with help of the method: readZipfile, the method would return the content of the file.
/**
*
*/
package com.example;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.util.Calendar;
import java.util.Random;
import java.util.zip.ZipEntry;
import java.util.zip.ZipException;
import java.util.zip.ZipFile;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;
/**
* @author Biplab Ray
*
*/
public class Test2 {
/**
* This method create the zip file and return the Absolute file path
*
* @param stringsFileName -
* Pass the File Name to be ZIP
* @return - A string object with absolute path
*/
public static String createZIP(String stringsFileName) {
String zipFileName = "";
zipFileName = createDirectory() + "/" +createId() + ".zip";
try {
File file = new File(zipFileName);
ZipOutputStream out = new ZipOutputStream(
new FileOutputStream(file));
out.putNextEntry(new ZipEntry(stringsFileName));
BufferedReader bufferedReader = new BufferedReader(new FileReader(
stringsFileName));
String string;
while ((string = bufferedReader.readLine()) != null) {
out.write(string.getBytes());
}
bufferedReader.close();
out.closeEntry();
out.flush();
out.close();
zipFileName = file.getAbsolutePath();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return zipFileName;
}
/**
* This Method Create Unique ID
*
* @return - A String object with the ID
*/
public static String createId() {
return Long.toHexString(Calendar.getInstance().getTimeInMillis()) + "-"
+ Integer.toHexString((new Random()).nextInt() % 65536);
}
/**
* This method Read the ZIP File and returns Content of the File in a String
* object
*
* @param stringFileName -
* Pass the File name to be read
* @return - Content of the file in a String object
*/
public static String readZipFile(String stringFileName) {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
try {
ZipFile zipFile = new ZipFile(new File(stringFileName));
ZipInputStream zipInputStream = new ZipInputStream(
new FileInputStream(stringFileName));
ZipEntry zipEntry = zipInputStream.getNextEntry();
BufferedReader reader = new BufferedReader(new FileReader(
stringFileName));
byte[] bs = reader.readLine().getBytes();
reader.close();
zipInputStream.closeEntry();
zipInputStream.close();
InputStream inputStream = zipFile.getInputStream(zipEntry);
int len;
while ((len = inputStream.read(bs)) > 0) {
byteArrayOutputStream.write(bs, 0, len);
}
inputStream.close();
byteArrayOutputStream.close();
} catch (ZipException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return byteArrayOutputStream.toString();
}
/**
* This method create a Directory in the HD in the defined directory e.g:
*
*
* @return - A String Object with details of Absolute Path of Directory
*/
public static String createDirectory() {
File file = null;
String stringDirPathName = "/usr/sap/";
if (stringDirPathName != null && !"".equals(stringDirPathName)) {
file = new File(stringDirPathName);
if (!file.mkdirs()) {
file.mkdirs();
}
}
if (null != file.getAbsolutePath()) {
stringDirPathName = file.getAbsolutePath();
}
return stringDirPathName;
}
/**
* This method create a New File and write contents to the Newly Created
* file
*
* @param stringFileContent -
* Pass the content of the file to be written
* @return - A string object with Absolute File Directory
*/
public static String writeToHD(String stringFileContent) {
String string = null;
try {
string = createDirectory() + "/" + createId() + ".xml";
File file = new File(string);
if (!file.exists()) {
file.createNewFile();
}
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(
file));
bufferedWriter.write(stringFileContent);
bufferedWriter.close();
string = file.getAbsolutePath();
} catch (IOException e) {
// TODO Auto-generated catch block
}
return string;
}
/**
* This method Delete the passed file.
*
* @param stringFileName -
* Pass the file path to be delete
*/
public static void deleteFile(String stringFileName) {
File file = new File(stringFileName);
if (file.exists() && file.isFile()) {
file.delete();
}
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>" +
"<Root><string_FirstName>Biplab</string_FirstName>" +
"<string_LastName>Ray</string_LastName>" +
"<child><string_StreetName>Raghnunath Pur</string_StreetName>" +
"<string_PINCODE>700156</string_PINCODE><string_State>West Bengal</string_State>" +
"<string_Country>India</string_Country></child></Root>";
/**
* Write To Hard Disk
* */
String string_absolute_Path = writeToHD(xml);
System.out.println(string_absolute_Path);
/**
* Create ZIP File
* */
String string_zip_name = createZIP(string_absolute_Path);
System.out.println(string_zip_name);
/**
* Delete The actual file
* */
deleteFile(string_absolute_Path);
/**
* Read the zip file
* */
String string_Read = readZipFile(string_zip_name);
System.out.println(string_Read);
}
}
Output would be like below:
E:\usr\sap\14bf2425ad6-d4a0.xml
E:\usr\sap\14bf2425ad6-237d.zip
<?xml version="1.0" encoding="UTF-8" standalone="yes"?><Root><string_FirstName>Biplab</string_FirstName><string_LastName>Ray</string_LastName><child><string_StreetName>Raghnunath Pur</string_StreetName><string_PINCODE>700156</string_PINCODE><string_State>West Bengal</string_State><string_Country>India</string_Country></child></Root>