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);
}
}
Related Tutorials/Questions & Answers:
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 all
files of
folders. The list() method is used to
find list of
files in a
folder How to extract a zip file to a folder in Linux?How to extract a
zip file to a
folder in Linux? Step by step command with
example to extract a
zip to specified
folder
In this tutorial we are going... content of a
zip file to a specified
directory. If you have a
zip file and you
Folder in Java.Folder in
Java. I have any problem how to make
Folder in
java program?
Can Anybody help me in this part
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
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 file zipJava 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
image save to folder in javaimage 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
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
schedule zipschedule 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 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...
files of one more
files or directories. Let's
learn
zip package of
java how to backup files and folder with Javahow 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
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
Chmod 777 to a folder and all contents, subfolders and all
files inside the
folder permission to
777. The chmod command is used to manage
files and
folder permissions in Linux.
This command allows the admin/users to add/update permissions to
files and
folders.
What
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
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 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
create foldercreate folder what code we should write as a web application that it create an empty
folder on client's desktop
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
Java- Save Image into project folder in web applicationJava- 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
code for serching files and folderscode 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 Encode 5 digit zip code - Java BeginnersEncode 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
Zip Code Validation - Java Interview QuestionsZip 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 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
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
How to make a zip file in javaDescription:
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 linux tar folder and subfolderslinux 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 I Choose a folder - Java Interview Questions do not explain clearly my question. I know how I get
files from a
folder.
My question How I select a
folder.
From this selected
folder i get the
files.
I know how I get
files but the problem How I choose a
folder.
Thanks
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
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 sending a zip file to servletsending 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
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
URL folder download to localURL
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
JavaScript regex validate validate Zip.JavaScript regex validate validate
Zip. JavaScript regex validate - how to validate
Zip?
<html>
<head>
<title>
Zip Code...;
function validate() {
var
zip = document.getElementById("
zip").value