Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Answers Viewqa Java-Beginners Java Util Zip. Zip files/folders inside a folder, without zipping the original source folder.
Login         

View Questions and Answers by Category

Advertisements


 
Have Programming Question? Ask it here!
 
 
 


Timothy
Java Util Zip. Zip files/folders inside a folder, without zipping the original source folder.
3 Answer(s)      3 years and 2 months ago
Posted in : Java Beginners


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


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


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
Zipping folder - Java Beginners
solve this.. zipping folder and all of its files and sub folders. thanking... to zip a user selected folder . this folder may contain any number of folder and sub folders. i saw an example in this site showing , how to zip a folder, i executed
Advertisements
Zipping folder - Java Beginners
solve this.. zipping folder and all of its files and sub folders. thanking... to zip a user selected folder . this folder may contain any number of folder and sub folders. i saw an example in this site showing , how to zip a folder, i executed
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
Folder in Java.
Folder in Java.  I have any problem how to make Folder in java program? Can Anybody help me in this part
Java file zip
Java file zip In this section, you will learn how to create a zip file. The ZIP file format is used for the distribution and storage of files. It is a compressed format file which takes less space then the original file. The zip file
copy file from folder to folder - Java Beginners
xml files from one folder to another my source file contains the field filename.... Suppose in the source file, file name is goodfile1.xml source folder is \\\\sap\\xi\\source\\Archive target folder is \\\\sap\\xi\\target I should copy
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 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
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
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
Java zip package - Learn how to use java zip package.
the zip and gzip files within java program. The zip format is very easy method... the archive files. The java zip package provides the API for reading and writing... package you can handle the zip files effectively within your java program. You
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
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
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
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
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
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
Encode 5 digit zip code - Java Beginners
Encode 5 digit zip code  I have an assignment to read in a 5 digit zip code, sum the digits, and come up with the check digit. I then need... as well as a | at the beginning and a | at the end. For example, the zip code 95014
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 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
Zip Code Validation - Java Interview Questions
Zip Code Validation  Hi, Anyone can please send me a javascript function of the following --> Indian postal Zip Code Validation (Only 6 digits... should not be greater than to date  Hi friend, Code for Validate Zip
How to make a zip file in java
Description: In this example we will discuss about how to create zip file from... zip file. Code: import java.io.*; import java.util.zip....;} } Output: When you compile and run this program it will create the zip
Read a zip file.
zip_entry_read() In this example the function zip_entry_read() will help in reading a zip file. First select a file to be opened. Name that file into zip file and direct the zipread command to read it through while loop. The zip_entry
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
How to extract zip file in java.
How to extract zip file in java. In this Example, we will discuss how to extract a ZIP file. We will use ZipInputStream class for creating input stream that take input from ZIP file. The ZipInputStream class is available
create folder
create folder  what code we should write as a web application that it create an empty folder on client's desktop
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
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
 

 

 

DMCA.com