Skip to content

Instantly share code, notes, and snippets.

@Denis1990
Last active August 29, 2015 14:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Denis1990/5ff6a98a4612456a6bac to your computer and use it in GitHub Desktop.
Save Denis1990/5ff6a98a4612456a6bac to your computer and use it in GitHub Desktop.
testEmptyFolderCompression
// taken from http://www.mkyong.com/java/how-to-compress-files-in-zip-format/
// and slightly modified to test my hypothesis.
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
public class AppZip
{
List<String> fileList;
private static final String OUTPUT_ZIP_FILE = "C:\\Users\\denis\\Desktop\\failest.zip";
private static final String SOURCE_FOLDER = "C:\\Users\\denis\\Desktop\\failtest";
AppZip(){
fileList = new ArrayList<String>();
}
public static void main( String[] args )
{
AppZip appZip = new AppZip();
appZip.generateFileList(new File(SOURCE_FOLDER));
if (appZip.fileList.isEmpty()) {
System.out.println("fileList is empty");
}
appZip.zipIt(OUTPUT_ZIP_FILE);
File resultFile = new File(OUTPUT_ZIP_FILE);
if (resultFile.exists() && !resultFile.isDirectory()) {
System.out.println("File " + OUTPUT_ZIP_FILE + " created");
}
}
/**
* Zip it
* @param zipFile output ZIP file location
*/
public void zipIt(String zipFile) {
byte[] buffer = new byte[1024];
try{
FileOutputStream fos = new FileOutputStream(zipFile);
ZipOutputStream zos = new ZipOutputStream(fos);
System.out.println("Output to Zip : " + zipFile);
for(String file : this.fileList){
System.out.println("File Added : " + file);
ZipEntry ze= new ZipEntry(file);
zos.putNextEntry(ze);
try {
FileInputStream in =
new FileInputStream(SOURCE_FOLDER + File.separator + file);
int len;
while ((len = in.read(buffer)) > 0) {
zos.write(buffer, 0, len);
}
in.close();
} catch(FileNotFoundException fne) {
fne.printStackTrace();
}
}
zos.closeEntry();
//remember close it
zos.close();
System.out.println("Done");
}catch(IOException ex){
ex.printStackTrace();
}
}
/**
* Traverse a directory and get all files,
* and add the file into fileList
* @param node file or directory
*/
public void generateFileList(File node) {
System.out.println("Inside generateFileList with file: " + node.getName());
//add file only
if(node.isFile()) {
fileList.add(generateZipEntry(node.getAbsoluteFile().toString()));
}
if(node.isDirectory()) {
System.out.println(node.getName() + " is a directory... going deeper!");
String[] subNote = node.list();
if (subNote.length == 0) {
System.out.println(node.getName() + " has no subfolders or files");
fileList.add(generateZipEntry(node.getAbsoluteFile().toString() + "/"));
}
for(String filename : subNote){
generateFileList(new File(node, filename));
}
}
}
/**
* Format the file path for zip
* @param file file path
* @return Formatted file path
*/
private String generateZipEntry(String file) {
return file.substring(SOURCE_FOLDER.length()+1, file.length());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment