Java provides an API to work with compressed file format, zip.
java.util.zip is the package that provides classes to compress and
decompress files in zip format.
This is how to compress a file to zip file format. If we want to compress myfile.txt to myfilezip.zip into the output folder, first myfile.txt is retrieved via FileInputStream. Data read through FileInputStream writes to ZipOutputStream. ZipEntry provides a way to put file entries to ZipOutputStream by putNextEntry method.
public class ConvertFiletoZip {
public void convertToZip(String file, String outputFolder) throws FileNotFoundException, IOException{
try{
//Gets myfile.txt as FileInputStream
File myfile = new File(file);
FileInputStream fileInputStream = new FileInputStream(myfile);
FileOutputStream outputStream = new FileOutputStream(outputFolder + "/myfilezip.zip");
//Creates ZipOutputStream from FileOutputStream
ZipOutputStream zipOutputStream = new ZipOutputStream(outputStream);
//Creates a ZipEntry and puts it to ZipOutputStream
ZipEntry zipEntry = new ZipEntry("myfile");
zipOutputStream.putNextEntry(zipEntry);
byte[] buffer = new byte[(int) myfile.length()];
int length = 0;
//Reads bytes from FileInputStream and writes to ZipOutputStream
while((length = fileInputStream.read(buffer))>0){
zipOutputStream.write(buffer, 0, length);
}
zipOutputStream.closeEntry();
zipOutputStream.close();
fileInputStream.close();
}catch (IOException e) {
System.out.println("Error occured when zip file is created");
}
}
public static void main(String[] args) throws FileNotFoundException, IOException{
ConvertFiletoZip readZipFile = new ConvertFiletoZip();
readZipFile.convertToZip("/home/kaushalya/myworkspace/ZipManipulation/myfile.txt","/home/kaushalya/myworkspace/ZipManipulation"
);
}
}
In the same way, we can read the zip file through ZipInputStream that is wrapped in FileInputStream. getNextEntry() method is used to get the entries of the zip file. The data of the file entry read to a bytes array is written through FileOutputStream to the output folder.
public class ConvertZiptoFile {
public static final String ZIP_FILE = "/home/kaushalya/myworkspace/ZipManipulation/files/files.zip";
public static final String OUTPUT_FOLDER = "/home/kaushalya/myworkspace/ZipManipulation/files";
public void convertToZipToFile(String zipFile, String outputFolder){
try {
File folder = new File(OUTPUT_FOLDER);
if(!folder.exists()){
folder.mkdir();
}
FileInputStream fileInputStream = new FileInputStream(zipFile);
ZipInputStream zipInputStream = new ZipInputStream(fileInputStream);
ZipEntry zipEntry = null;
while((zipEntry = zipInputStream.getNextEntry()) != null){
System.out.println("File entry : " + zipEntry.getName());
File unzippedFile = new File(folder+"/"+zipEntry.getName());
int length = (int) unzippedFile.length();
byte[] buffer = new byte[length];
FileOutputStream fileOutputStream = new FileOutputStream(unzippedFile);
int len =0;
while((len =zipInputStream.read(buffer))>0){
fileOutputStream.write(buffer, 0, len);
}
zipEntry = null;
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args){
ConvertZiptoFile convertZiptoFile = new ConvertZiptoFile();
convertZiptoFile.convertToZipToFile(ZIP_FILE, OUTPUT_FOLDER);
}
}
Kaushalya's blog
Monday, February 4, 2013
Friday, January 25, 2013
Singleton Design Pattern - Java
There may be situations that we need
exactly one instance of a class to perform some functionalities.
Singleton design pattern ensures that only one instance of a class is
created and provides a global point of access to the object.
Public class MySingleton(){
private static MySingleton
singletonInstance;
private MySingleton(){
}
public static MySingleton
getSigleton(){
if(singletonInstance = null)
singletonInstance = new MySingleton()
return singletonInstance;
}
}
If we make the constructor of
MySingleton class private, The outside classes can't create instances
of MySingleton class. We can create instances only within MySingleton
class. Therefore, public static getter method is created to give the
instance to outside.
In the situations of using multi
threading, getter method has to be put as synchronized because if two
threads may access the method at the same time and may create two
instances of MySingleton class. But using synchronized keyword may
reduce the performance of the program.
Public class MySingleton(){
private static MySingleton
singletonInstance;
private MySingleton(){
}
public static synchronized MySingleton
getSigleton(){
if(singletonInstance = null)
singletonInstance = new MySingletn()
return singletonInstance;
}
}
The other option to create a Singleton
class is to create the instance when class is loaded. Multi
threading problems may not occur because before any of threads access
the instance is already created when the class is loaded.
Public class MySingleton(){
private static final MySingleton
singletonInstance = new MySingleton()
private MySingleton(){
}
public static MySingleton
getMySingletn (){
return singletonInstance;
}
}
According to the performance of your
program and needs, you can select which method would be better to
create the Singleton class. If you are using several class loaders,
it may create more than one instance. Singleton class cannot be
subclassed because it has a private constructor.
Best way to create a
sigleton class is to use enum as described in Effective Java Reloaded by
Joshua Bloch. Enum guarantees only one instance and thread safety. As the below code, we can declare enum Singleton easily.
public enum MySigleton{
INSTANCE;
public void execute(){
//Other functionalities
}
}
We can get the singleton instance by using MySingleton.INSTANCE.
We can get the singleton instance by using MySingleton.INSTANCE.
Wednesday, February 23, 2011
Web MVC framework-Controllers
Controllers in Web MVC framework, interpret user inputs and transform such input in to a view that is to be viewed by the user. Spring has implemented several controllers in a very abstractive way. They are form-specific controllers, command-based controllers and controllers that execute wizard-style logic. Spring's basic for the controllers is the org.springframework.web.servlet.mvc.Controller interface.
public interface Controller {
ModelAndView handleRequest(
HttpServletRequest request,
HttpServletResponse response) throws Exception;
}
handleRequest method is responsible for handling a request and returning an appropriate modelandview.
AbstractController - Spring controllers inherit the AbstractController to get a basic infrastructure. Then we have to override the method handleRequestInternal(HttpServletRequest, HttpServletResponse). Here is an example.
public class MyController extends AbstractController {
public ModelAndView handleRequestInternal(
HttpServletRequest request,
HttpServletResponse response) throws Exception {
ModelAndView mav = new ModelAndView("hello");
mav.addObject("message", "Hello World!");
return mav;
}
}
ParameterizableViewController - This is same as the AbstractController except we can specify the view name that it will return in the web application context.
UrlFilenameViewController - This checks the URL and retrieves the file name of the file request and uses that as the view name.
MultiActionController - This allows the aggregation of multiple request handling methods to one controller.
Command Controllers
Command controllers allow to interact with data objects and dynamically bind the parameters from the HttpServletRequest to the data object specified.
AbstractCommandController - This is a command controller that is capable of binding request parameters to a data object. This controller does not provide form functionality.
AbstractFormController - Provides the form submission support.
SimpleFormController - A form controller that provides even more support when creating a form with a corresponding command object.
public interface Controller {
ModelAndView handleRequest(
HttpServletRequest request,
HttpServletResponse response) throws Exception;
}
handleRequest method is responsible for handling a request and returning an appropriate modelandview.
AbstractController - Spring controllers inherit the AbstractController to get a basic infrastructure. Then we have to override the method handleRequestInternal(HttpServletRequest, HttpServletResponse). Here is an example.
public class MyController extends AbstractController {
public ModelAndView handleRequestInternal(
HttpServletRequest request,
HttpServletResponse response) throws Exception {
ModelAndView mav = new ModelAndView("hello");
mav.addObject("message", "Hello World!");
return mav;
}
}
ParameterizableViewController - This is same as the AbstractController except we can specify the view name that it will return in the web application context.
UrlFilenameViewController - This checks the URL and retrieves the file name of the file request and uses that as the view name.
MultiActionController - This allows the aggregation of multiple request handling methods to one controller.
Command Controllers
Command controllers allow to interact with data objects and dynamically bind the parameters from the HttpServletRequest to the data object specified.
AbstractCommandController - This is a command controller that is capable of binding request parameters to a data object. This controller does not provide form functionality.
AbstractFormController - Provides the form submission support.
SimpleFormController - A form controller that provides even more support when creating a form with a corresponding command object.
Subscribe to:
Posts (Atom)