Java Util Zip. Zip files/folders inside a folder, without zipping the original source folder.

Ads
 

Java Util Zip. Zip files/folders inside a folder, without zipping the original source folder.

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 =/

View Answers

September 27, 2011 at 1:00 AM

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);
  }
}

} }


September 27, 2011 at 1:01 AM

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);
      }
    }
  }
}

September 27, 2011 at 11:16 AM

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);
  }
}

Ads









Related Tutorials/Questions & Answers:
Java Util Zip. Zip files/folders inside a folder, without zipping the original source folder.
Java Util Zip. Zip files/folders inside a folder, without zipping the original source folder.   import java.io.File; import... the method a very static scenario. I want to take the files inside the folder
Zip Folder Using Java
; In this example we are going to create a zip folder and all files contained in this folder are zipped by using java. Here we will learn how to compress files... of folders. The list() method is used to find list of files in a folder. By using
Advertisements
ModuleNotFoundError: No module named 'zip-folder-sync'
ModuleNotFoundError: No module named 'zip-folder-sync'  Hi, My... named 'zip-folder-sync' How to remove the ModuleNotFoundError: No module named 'zip-folder-sync' error? Thanks   Hi, In your python
Folder in Java.
Folder in Java.  I have any problem how to make Folder in java program? Can Anybody help me in this part
Video Tutorial: How to check if a folder exists in Java through a source code
How to check if a folder exists in Java through a source code Java is an all... if our folder exists in Java by the help of source code in a few simple steps... facilities. Here is the video tutorial of "How to check if a folder exists in Java
java program to take the backup of folders and copy the folder in different location without deleting the existing one
java program to take the backup of folders and copy the folder in different location without deleting the existing one  java program to take the backup of folders and copy the folder in different location without deleting
Java lock file or folder.
Java lock file or folder.  Is it possible to create a desktop application to lock or encrypt file or folder in windows by using java code?? if possible can provide some reference?? because i can't find it... thanks
image save to folder in java
image save to folder in java  Hi, I am working with java. In my application i want to give facility to user to add and change image. I use open... folder and that path stored into database, for furhter retrivation. Please guide me
how to backup files and folder with Java
how to backup files and folder with Java  Does anyone know to create a backup with java?. backup files and folders from my computer to an external...:\\Documents and Settings\\bharat\\Desktop\\bipul\\New Folder\\TableExample.java
java program to copy the contents of one folder to another folder
java program to copy the contents of one folder to another folder  java program to copy the contents of one folder to another folder   Have a look at the following link: Java Copy one folder to another
Create zip file in Java
, the user can "extract" the original files from the ZIP file using a ZIP... applications. It is also possible to zip and unzip the files from your Java... Create zip file in Java     
Java get Folder Size
of specified folder and also the number of files and folders that are presented... Java get Folder Size      ... the size of folder. The totalFolder++ counts the total folders and totalFile++ counts
schedule zip
schedule zip  Java Code to automatically get and zip only .txt files from folders for every 5 days and also to send the zip files to database tables
Creating a ZIP file in Java
Creating a ZIP file in Java   ... of data compression. Zip file contains many files in compressed format. You can say the files are stored in the zip format with compression. The file which
How to convert multiple files in java to .zip format
How to convert multiple files in java to .zip format  i receive multiple files from a remote URL as for (String reportid : reportList...); } i want to convert the data from inputStream into Zip Stream and so
Java Zip Package
to include classes for manipulating ZIP files as part of the standard Java APIs... Java Zip Package      ... and writing the standard and compressed files or archives in the ZIP and GZIP file
Creat a folder, inside it upload and download files in jsp and mysql
Creat a folder, inside it upload and download files in jsp and mysql  Create one or more folder .inside it we can upload and download multiple files... to database. The uploaded files will then displayed on the browser and will allow the user
Java- Save Image into project folder in web application
Java- Save Image into project folder in web application  I have a requirement of saving the image into project folder .. which is a web application. I am not able to figure out how to find the absolute path of my folder which I
Zip Code
Zip Code   I have 4 excel file and i want to zip that files also i need password protect when i open that file please write that code for local machine also write in using javascript
code for serching files and folders
code for serching files and folders  i want to create a code in java... or folder presented in local system...if we go inside that file and there are more folder present inside that folder we can use similar thread to work on inside folder
Zip File Using Java
, WinZip etc. It is also possible to zip and unzip the files from your Java applications. This example shows how we zip a file through a java program...; This example shows how to create a zip file in java. In other words, we
create folder
create folder  what code we should write as a web application that it create an empty folder on client's desktop
ModuleNotFoundError: No module named 'zip'
ModuleNotFoundError: No module named 'zip'  Hi, My Python program is throwing following error: ModuleNotFoundError: No module named 'zip' How to remove the ModuleNotFoundError: No module named 'zip' error
linux tar folder and subfolders
linux tar folder and subfolders  Hi, I am new to Linux operating system. I have a directory which contains many files in several directories and sub-directories. There are many files in each directory and sub-directories. So
how to upload a file of apk format in a folder from servlet
how to upload a file of apk format in a folder from servlet  How to upload a file of apk format to a folder from servlet, i am trying to do, its getting uploaded as .zip file and not apk file
URL folder download to local
URL folder download to local  I have a requirement to download all the folders and files inside from a SVN link to my local directory. Can you help how it could be done
find zip file length lessthen 5MB
File folder = new File(source); if(folder.exists...find zip file length lessthen 5MB  import java.io.BufferedReader... InputStreamReader(System.in)); public static int buffer = 5242890; String source
create a folder in jsp page
create a folder in jsp page  i want to create a folder in jsp page And it can be delete aslo
Issue in Converting .zip file to .jar file?
Issue in Converting .zip file to .jar file?  I have a folder called "printpdf.jar". I have changed the .jar to .zip and did some customizations. After which i had "zipped" it again and made as ".jar". Now the final file
sending a zip file to servlet
sending a zip file to servlet  I have created a .zip file in a servlet on the local system(no static ip). The .zip file contains xml files. I have to send it to another servlet which is in a server(has a static ip). I have done
Artifacts of org.elasticsearch.distribution.integ-test-zip
List of Artifacts of org.elasticsearch.distribution.integ-test-zip maven depenency

Ads