I have one file created by 7zip program. I used deflate method to compress it. Now I want to create the same archive (with the same MD5sum) in java
Here is a function that you pass the absolute path it will create a zip file with the same name as the directory (under which you want zip of all the sub folder and files, everything !!) and return true on success and false on exception if any.
public class FileUtil {
final static int BUFFER = 2048;
private static Logger log = Logger.getLogger(FileUtil.class);
public static boolean createZipArchive(String srcFolder) {
try {
BufferedInputStream origin = null;
FileOutputStream dest = new FileOutputStream(new File(srcFolder+ ".zip"));
ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(dest));
byte data[] = new byte[BUFFER];
File subDir = new File(srcFolder);
String subdirList[] = subDir.list();
for(String sd:subdirList)
{
// get a list of files from current directory
File f = new File(srcFolder+"/"+sd);
if(f.isDirectory())
{
String files[] = f.list();
for (int i = 0; i < files.length; i++) {
System.out.println("Adding: " + files[i]);
FileInputStream fi = new FileInputStream(srcFolder + "/"+sd+"/" + files[i]);
origin = new BufferedInputStream(fi, BUFFER);
ZipEntry entry = new ZipEntry(sd +"/"+files[i]);
out.putNextEntry(entry);
int count;
while ((count = origin.read(data, 0, BUFFER)) != -1) {
out.write(data, 0, count);
out.flush();
}
}
}
else //it is just a file
{
FileInputStream fi = new FileInputStream(f);
origin = new BufferedInputStream(fi, BUFFER);
ZipEntry entry = new ZipEntry(sd);
out.putNextEntry(entry);
int count;
while ((count = origin.read(data, 0, BUFFER)) != -1) {
out.write(data, 0, count);
out.flush();
}
}
}
origin.close();
out.flush();
out.close();
} catch (Exception e) {
log.info("createZipArchive threw exception: " + e.getMessage());
return false;
}
return true;
}
}