Wednesday, July 8, 2009

How to delete recursively empty folder using java, Recursively Delete Empty Folders

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class DeleteEmptyFolder {
public static void main(String[] args) throws IOException {
deleteEmptyFolders("C:\\temp");
}

public static void deleteEmptyFolders(String folderName) throws FileNotFoundException {
File aStartingDir = new File(folderName);
List<File> emptyFolders = new ArrayList<File>();
findEmptyFoldersInDir(aStartingDir, emptyFolders);
List<String> fileNames = new ArrayList<String>();
for (File f : emptyFolders) {
String s = f.getAbsolutePath(); fileNames.add(s);
}
for (File f : emptyFolders) {
boolean isDeleted = f.delete();
if (isDeleted) {
System.out.println(f.getPath() + " deleted");
}
}
}

public static boolean findEmptyFoldersInDir(File folder, List<File> emptyFolders) {
boolean isEmpty = false;
File[] filesAndDirs = folder.listFiles();
List<File> filesDirs = Arrays.asList(filesAndDirs);
if (filesDirs.size() == 0) { isEmpty = true; }
if (filesDirs.size() > 0) {
boolean allDirsEmpty = true;
boolean noFiles = true;
for (File file : filesDirs) {
if (!file.isFile()) {
boolean isEmptyChild = findEmptyFoldersInDir(file, emptyFolders);
if (!isEmptyChild) { allDirsEmpty = false; }
}
if (file.isFile()) { noFiles = false; }
}
if (noFiles == true && allDirsEmpty == true) { isEmpty = true; }
} if (isEmpty) { emptyFolders.add(folder); }
return isEmpty;
}
}

How to get recursively listing all files in directory using Java

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class AllFilesInFolder {
public static void main(String[] args) throws FileNotFoundException {
String folderName = "C:\\temp";
List<String> fileNames = getAllFiles(folderName);
System.out.println("All Files :: " + fileNames);
}

public static List<String> getAllFiles(String folderName) throws FileNotFoundException {
File aStartingDir = new File(folderName);
List<File> result = getFileListingNoSort(aStartingDir);
List<String> fileNames = new ArrayList<String>();
for (File f : result) {
String s = f.getAbsolutePath();
fileNames.add(s);
}

Collections.sort(result);
Collections.sort(fileNames);
return fileNames;
}

public static List<File> getFileListingNoSort(File aStartingDir) throws FileNotFoundException {
List<File> result = new ArrayList<File>();
File[] filesAndDirs = aStartingDir.listFiles();
List<File> filesDirs = Arrays.asList(filesAndDirs);
for (File file : filesDirs) {
if (!file.isDirectory()) result.add(file);
if (!file.isFile()) {
List<File> deeperList = getFileListingNoSort(file);
result.addAll(deeperList);
}
}
return result;
}
}

Monday, July 6, 2009

how to unzip file in java; Java Unzip file,

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

public class MakeUnZip {
public static void main(String argv[]) {
String zipFileName = "src\\myZip.zip";
unZip(zipFileName);
}

public static void unZip(String zipFileName) {
int BUFFER = 2048;
try {
BufferedOutputStream dest = null;
FileInputStream fileInputStream = new FileInputStream(zipFileName);
ZipInputStream zipInputStream = new ZipInputStream(new BufferedInputStream(fileInputStream));
ZipEntry zipEntry;
int count=0;
while ((zipEntry = zipInputStream.getNextEntry()) != null) {
System.out.println("Extracting File Name :: " + zipEntry);
count++; int length;
byte data[] = new byte[BUFFER];
FileOutputStream fileOutputStream = new FileOutputStream(zipEntry.getName());
dest = new BufferedOutputStream(fileOutputStream, BUFFER);
while ((length = zipInputStream.read(data, 0, BUFFER)) != -1) {
dest.write(data, 0, length);
}

dest.flush();
dest.close();
}

zipInputStream.close();
System.out.println("Total "+count+ " Files Unziped Successfully ");
} catch (Exception e) { e.printStackTrace(); }
}
}

Sunday, July 5, 2009

Get contents of a ZIP file in Java, how to get file names in zip file in java

import java.io.File;
import java.io.IOException;
import java.util.Enumeration;
import java.util.zip.ZipFile;

public class ZipContents {
public static void main(String[] args) {
File zipFileName = new File("src\\myZip.zip");
getContentsInZip(zipFileName);
}

public static void getContentsInZip(File zipFileName){
try{ ZipFile zipFile = new ZipFile(zipFileName);
Enumeration em = zipFile.entries();
for (Enumeration enumer = zipFile.entries(); enumer.hasMoreElements();) {
System.out.println(enumer.nextElement());
}
}catch(IOException e){ e.printStackTrace(); }
}
}

How to make Zip file using Java, Creating a ZIP file in Java

To run this tutorial
1. Create one folder src
2. Put test1.txt and test2.txt in src folder.
3. After run this code, you will get myZip.zip file in src folder.

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class MakeZip {
public static void main(String[] args) {
String[] filesToZip = {"src\\test1.txt","src\\test2.txt"};
//String[] filesToZip = {"src\\test1.txt"};
String ZipedFileName = "src\\myZip.zip";
zipConversion(filesToZip, ZipedFileName);
}

public static void zipConversion(String[] files, String ZipedFileName){
byte[] buffer = new byte[1024];
try{
FileOutputStream outputFile = new FileOutputStream(ZipedFileName);
ZipOutputStream zipFile = new ZipOutputStream(outputFile);
for(int i=0;i<files.length;i++){
FileInputStream inFile = new FileInputStream(files[i]);
zipFile.putNextEntry(new ZipEntry(files[i]));
int length;
while ((length = inFile.read(buffer)) > 0) {
zipFile.write(buffer, 0, length); }

zipFile.closeEntry();
inFile.close();
}

zipFile.close();
System.out.println("Files Ziped Successfully");
}catch(IOException e){ e.printStackTrace(); }
}
}

You can either give one file to zip or any number of files in fileToZip string array.