import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
public class FileUtil
{
public static void zip(){}
public static void unzip(){}
public static boolean copyFile(String infile,String outfile) throws Exception {
try{
FileInputStream source = new FileInputStream(infile);
FileOutputStream copy = new FileOutputStream(outfile);
int byteCount = 0;
while (true) {
int data = source.read();
if (data < 0)
break;
copy.write(data);
byteCount++;
}
source.close();
copy.close();
return isFileExist(outfile);
}catch(Exception e){
e.printStackTrace();
return false;
}
}
/**
* Check if the file exists or not
* @param filepath
* @return boolean true if file exists false otherwise.
*/
public static boolean isFileExist(String filepath) {
boolean recfIdExist = false;
try {
File fileInput = new File(filepath);
recfIdExist = fileInput.exists();
} catch (Exception e) {
}
return recfIdExist;
}
/**
* unzip the input file
* @param filePath
* @param fileName
* @return
*/
public static String unzip(String filePath) throws Exception{
String extension = null;
ZipFile zipFile = null;
FileOutputStream fout = null;
InputStream inputStream = null;
//String fileName=filePath.substring(filePath.lastIndexOf("/")+1,filePath.length());
String fileName=extractFileName(filePath);
filePath=filePath.substring(0,filePath.lastIndexOf("/")+1);
try{
zipFile = new ZipFile(filePath+fileName);
Enumeration enumeration = zipFile.entries();
while(enumeration.hasMoreElements()){
ZipEntry zipEntry = (ZipEntry)enumeration.nextElement();
String unzipFile = zipEntry.getName();
extension = extractExtension(unzipFile);
unzipFile=unzipFile.substring(0,unzipFile.lastIndexOf("."));
String extractedFileName = filePath + unzipFile+extension ;
fout = new FileOutputStream( extractedFileName );
inputStream = zipFile.getInputStream(zipEntry);
int data = 0;
byte b[] = new byte[8092];
while ((data = inputStream.read(b)) > 0 ) {
fout.write(b,0,data);
}
}
}catch(Exception error){
throw error;
}
finally {
try {
if (inputStream != null) {
inputStream.close();
}
if (fout != null) {
fout.close();
}
if (zipFile != null) {
zipFile.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return filePath+fileName+extension;
}
// Extract the extension from file name
public static String extractExtension(String fileName) {
int indexValue = fileName.indexOf(".");
if( indexValue != -1) {
return fileName.substring(indexValue);
}else return fileName;
}
// Extract the file name
public static String extractFileName(String fileName) {
return fileName.substring(fileName.lastIndexOf("/")+1);
}
/**
* Create a directory name for current date in MMddyyyy format
* @return
*/
public static String getBaseDirNameByDate(String savePath) {
SimpleDateFormat sdf=new SimpleDateFormat("MMddyyyy");
String strDate = (sdf.format(new Date())).toString();
savePath=savePath+"/"+strDate;
boolean exists = (new File(savePath)).exists();
if (exists) {
System.out.println("Directory is Already Exists");
} else {
boolean success = (new File(savePath)).mkdirs();
System.out.println("Directory Does not Exists "+success);
}
return savePath;
}
/**
* Checks if the file is a Excel file
* @param fileName
* @return
*/
public static boolean isExcelFile(String fileName) {
if(fileName == null ) {
return false;
} else {
return fileName.endsWith(".xls");
}
}
/**
* Checks if the file is a CSV file
* @param fileName
* @return
*/
public static boolean isCSVFile(String fileName) {
if(fileName == null ) {
return false;
} else {
return fileName.endsWith(".csv");
}
}
/**
* Checks if the file is a zip file
* @param fileName
* @return
*/
public static boolean isZipFile(String fileName) {
if(fileName == null ) {
return false;
} else {
return fileName.endsWith(".zip");
}
}
public static boolean deleteFile(String fileName) {
File f = new File(fileName);
// Make sure the file or directory exists and isn't write protected
/*if (!f.exists())
throw new IllegalArgumentException(
"Delete: no such file or directory: " + fileName);
if (!f.canWrite())
throw new IllegalArgumentException("Delete: write protected: "
+ fileName);
// If it is a directory, make sure it is empty
if (f.isDirectory()) {
String[] files = f.list();
if (files.length > 0)
throw new IllegalArgumentException(
"Delete: directory not empty: " + fileName);
}*/
// Attempt to delete it
boolean success = f.delete();
if (!success)
return true;
return false;
}
}
Thursday, October 8, 2009
Subscribe to:
Post Comments (Atom)

No comments:
Post a Comment