import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream;
public class FolderZipper { public static void main() throws Exception { zipFolder("Temp", "Final\final.jar"); }
static public void zipFolder(String srcFolder, String destZipFile) throws Exception { ZipOutputStream zip = null; FileOutputStream fileWriter = null;
fileWriter = new FileOutputStream(destZipFile); zip = new ZipOutputStream(fileWriter); addFolderToZip("", srcFolder, zip); zip.flush(); zip.close(); }static private void addFileToZip(String path, String srcFile, ZipOutputStream zip) throws Exception {
File folder = new File(srcFile); if (folder.isDirectory()) { addFolderToZip(path, srcFile, zip); } else { byte[] buf = new byte[1024]; int len; //srcFile = srcFile.substring(5); System.out.println(path + " " + srcFile); FileInputStream in = new FileInputStream(srcFile); zip.putNextEntry(new ZipEntry(path + "/" +folder.getName())); while ((len = in.read(buf)) > 0) { zip.write(buf, 0, len); } } }
static private void addFolderToZip(String path, String srcFolder, ZipOutputStream zip) throws Exception { File folder = new File(srcFolder);
for (String fileName : folder.list()) { System.out.println(fileName); if (path.equals("")) { addFileToZip(folder.getName(), srcFolder + "/" + fileName, zip); } else { addFileToZip(path + "/" + folder.getName(), srcFolder + "/" +fileName, zip); } } } }
This is my Zipping code and i'll tell you i am having such a time, i give the method a very static scenario. I want to take the files inside the folder TEMP and zip into a file named Final.jar located in the folder Final.
Now this code works wonderful, its another example from a different website. The only issue is, it zips everything into the Jar under the folder "TEMP" and not the root of the jar. I've been attempting to modify it using the JavaDocs and other examples, but its come to me just randomly guessing now so =/
Erg... Not sure what happened to the code above, i didn't use a quoteblock (least i thought i didnt) Here is the code with no blocks.
import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream;
public class FolderZipper { public static void main() throws Exception { zipFolder("Temp", "Final\final.jar"); }
static public void zipFolder(String srcFolder, String destZipFile) throws Exception { ZipOutputStream zip = null; FileOutputStream fileWriter = null;
fileWriter = new FileOutputStream(destZipFile); zip = new ZipOutputStream(fileWriter); addFolderToZip("", srcFolder, zip); zip.flush(); zip.close();
}
static private void addFileToZip(String path, String srcFile, ZipOutputStream zip) throws Exception {
File folder = new File(srcFile); if (folder.isDirectory()) { addFolderToZip(path, srcFile, zip); } else { byte[] buf = new byte[1024]; int len; //srcFile = srcFile.substring(5); System.out.println(path + " " + srcFile); FileInputStream in = new FileInputStream(srcFile); zip.putNextEntry(new ZipEntry(path + "/" + folder.getName())); while ((len = in.read(buf)) > 0) { zip.write(buf, 0, len); } }
}
static private void addFolderToZip(String path, String srcFolder, ZipOutputStream zip) throws Exception { File folder = new File(srcFolder);
for (String fileName : folder.list()) { System.out.println(fileName); if (path.equals("")) { addFileToZip(folder.getName(), srcFolder + "/" + fileName, zip); } else { addFileToZip(path + "/" + folder.getName(), srcFolder + "/" + fileName, zip); } }
} }
You know what? Apparently it hates me :( One last shot, looking to delete things. The preview doesn't even show this
import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; public class FolderZipper { public static void main() throws Exception { zipFolder("Temp", "Final\\final.jar"); } static public void zipFolder(String srcFolder, String destZipFile) throws Exception { ZipOutputStream zip = null; FileOutputStream fileWriter = null; fileWriter = new FileOutputStream(destZipFile); zip = new ZipOutputStream(fileWriter); addFolderToZip("", srcFolder, zip); zip.flush(); zip.close(); } static private void addFileToZip(String path, String srcFile, ZipOutputStream zip) throws Exception { File folder = new File(srcFile); if (folder.isDirectory()) { addFolderToZip(path, srcFile, zip); } else { byte[] buf = new byte[1024]; int len; //srcFile = srcFile.substring(5); System.out.println(path + " " + srcFile); FileInputStream in = new FileInputStream(srcFile); zip.putNextEntry(new ZipEntry(path + "/" + folder.getName())); while ((len = in.read(buf)) > 0) { zip.write(buf, 0, len); } } } static private void addFolderToZip(String path, String srcFolder, ZipOutputStream zip) throws Exception { File folder = new File(srcFolder); for (String fileName : folder.list()) { System.out.println(fileName); if (path.equals("")) { addFileToZip(folder.getName(), srcFolder + "/" + fileName, zip); } else { addFileToZip(path + "/" + folder.getName(), srcFolder + "/" + fileName, zip); } } } }
import java.io.*; import java.util.zip.*; public class CreateZip { public static int buffer = 10240; protected void createZip(File zipFile, File[] listFiles) { try { byte b[] = new byte[buffer]; FileOutputStream fout = new FileOutputStream(zipFile); ZipOutputStream out = new ZipOutputStream(fout); for (int i = 0; i < listFiles.length; i++) { if (listFiles[i] == null || !listFiles[i].exists()|| listFiles[i].isDirectory()) System.out.println(); ZipEntry addFiles = new ZipEntry(listFiles[i].getName()); addFiles.setTime(listFiles[i].lastModified()); out.putNextEntry(addFiles); FileInputStream fin = new FileInputStream(listFiles[i]); while (true) { int len = fin.read(b, 0, b.length); if (len <= 0) break; out.write(b, 0, len); } fin.close(); } out.close(); fout.close(); System.out.println("Zip File is created successfully."); } catch (Exception ex) {} } public static void main(String[]args){ CreateJar jar=new CreateJar(); File folder = new File("C://Answers//Examples"); File[] files = folder.listFiles(); File file=new File("C://Answers//Examples//Examples.zip"); jar.createZip(file, files); } }