How To Compress Zip Files in Java
How To Compress Zip Files in Java
En el capitulo de hoy vamos a ver como comprimir archivos en formato ZIP. Si bien a
Sometimes we have files that cannot be compressed too much; it's always very useful to zip them.
files by grouping them into a large file.
import java.util.zip.*;
import java.io.*;
/**
*
* @author GeekZero
*/
public class Files {
// buffer
byte[] buffer= new byte[BUFFER_SIZE];
try {
// file to compress
fis= new FileInputStream(pFile);
// container file of the zip
fos= new FileOutputStream(pZipFile);
compressed file
zipos= new ZipOutputStream(fos);
ZipEntry zipEntry = new ZipEntry(pFile);
zipos.putNextEntry(zipEntry);
int len= 0;
zippear
while (len= fis.read(buffer,0, BUFFER_SIZE))
!= -1)
zipos.write(buffer, 0, len);
dump the memory to disk
zipos.flush();
} catch (Exceptione) {
throw e;
} finally {
we close the files
zipos.close();
fis.close();
fos.close();
} // end try
} // end Zippear
try {
fis= new FileInputStream(pZipFile);
zipis= new ZipInputStream(new
BufferedInputStream(fis));
if (zipis.getNextEntry() != null) {
int len= 0;
byte[] buffer= new byte[BUFFER_SIZE];
fos= new FileOutputStream(pFile);
bos= new BufferedOutputStream(fos,
BUFFER_SIZE);
while((len= zipis.read(buffer,0,
BUFFER_SIZE) != -1)
bos.write(buffer, 0, len);
bos.flush();
} else {
throw new ExceptionThe zip did not contain
any file)
} // end if
} catch (Exceptione) {
throw e;
} finally {
bos.close();
zipis.close();
fos.close();
fis.close();
} // end try
} // end UnZip
example of use
public static void main(String[] args) throws Exception {
try {
Archivos arch= new Files();
arch.Zippear("devtroce.jpg","devtroce.zip");
SystemCompressed!
arch.UnZip("devtroce.zip","new_devtroce.jpg");
SystemUnzipped!
} catch (Exceptione) {
e.printStackTrace();
}
}
}// end class
Another method
Hello guys, how are you? I hardly found many tutorials while browsing the web.
how to compress files in java with a password and without a password today I will teach you in
various entries on how to compress files without a password and with a password using the
NetBeans platform in Java let's start :)
Download library
then we add the .jar library by right-clicking on the library folder --> add
jar file
after that we are going to create a new class I'm going to call it add_a_un_zip and
we added the following code:
import java.io.File;
import java.util.ArrayList;
import net.lingala.zip4j.core.ZipFile;
import net.lingala.zip4j.exception.ZipException;
import net.lingala.zip4j.model.ZipParameters;
import net.lingala.zip4j.util.Zip4jConstants;
/**
*
* @author andres2288
*/
add a file to the zip but can also create the zip from scratch
public class add_a_un_zip {
public add_a_un_zip () {
try {
ZipFile zipFile = new
ZipFile("C:/Users/andres2288/Documents/compression/andres2288.zip");
parameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL);
zipFile.addFiles(filesToAdd, parameters);
} catch (ZipException e) {
e.printStackTrace();
}
}
We have the default constructor and there we place the code there is an exception catch
but be careful it is not from netbeans it is from the default zip library that's why when creating a
we can make a mistake in the exception since it is not Exception e but rather
It's a ZipException, in the code we use an ArrayList to add how many files.
we want and define parameters of the library to set parameters for compression
such as the level of compression if maximum, normal, or low and that it is constant,
in the part where it says ZipFile zipFile = new
ZipFile("C:/Users/andres2288/Documents/compression/andes2288.zip"); is the path
where the new compressed file will be created or the existing andres2288.zip will be
new file or if it already exists, compress the plain text file inside where it says
filesToAdd.add(new
File("C:/Users/andres2288/Documents/compression/ZipTest/sample.txt"); is the
file to compress can compress anything you want up to a
mp3 file and you can add more files by repeating that line that's all :)
add a folder to a compressed file with java
import java.io.File;
import java.util.ArrayList;
import net.lingala.zip4j.core.ZipFile;
import net.lingala.zip4j.exception.ZipException;
import net.lingala.zip4j.model.ZipParameters;
import net.lingala.zip4j.util.Zip4jConstants;
/**
*
* @author andres2288
*/
public class add_only_one_folder {
//add external files to the compressed file but creating a folder
desired a chimba
public add_only_one_folder() {
try {
ZipFile zipFile = new
ZipFile("C:/Users/andres2288/Documents/compression/andres2288.zip");
parameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL);
parameters.setRootFolderInZip("agregado/");
zipFile.addFiles(filesToAdd, parameters);
} catch (ZipException e) {
e.printStackTrace();
}
Well guys, here are several entries where we use various functions of this.
library the library at the end I leave you the download link the following code is for
compress files with java and put a password for security only puts it
password for the file that you wish to compress the rest not but however
You can compress the files you want since we use an ArrayList.
You can play with this library; it has several functions, and I will explain them to you.
each entry:
The first thing we need to do is create a new class.
we will call add_a_zip_with_password
and in that class we put the following code:
import java.io.File;
import java.util.ArrayList;
import net.lingala.zip4j.core.ZipFile;
import net.lingala.zip4j.exception.ZipException;
import net.lingala.zip4j.model.ZipParameters;
import net.lingala.zip4j.util.Zip4jConstants;
try {
ZipFile zipFile = new
ZipFile("C:/Users/andres2288/Documents/compression/andes2288.zip");
parameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL);
parameters.setEncryptFiles(true);
parameters.setEncryptionMethod(Zip4jConstants.ENC_METHOD_AES);
parameters.setAesKeyStrength(Zip4jConstants.AES_STRENGTH_256);
parameters.setPassword("123");
zipFile.addFiles(filesToAdd, parameters);
} catch (ZipException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
new add_a_un_zip_with_password();
}
}
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
try {
InputStream in = new FileInputStream(source);
OutputStream out = new
FileOutputStream(destination);
in.close();
out.close();
} catch (IOException ioe) {
ioe.printStackTrace();
}
import java.io.File;
import javazoom.jlgui.basicplayer.BasicPlayer;
public Reproductor() {
player = new BasicPlayer();
}
public void coge(String y){
}
public void Play() throws Exception {
player.play();
}
OutputStream os = ...;
zos.write(...);
zos.closeEntry();
zos.write(...);
zos.closeEntry();
zos.close();
The method that performs all the magic is makeZip(String fileName) which creates a zip file.
of the respective file or directory named fileName + ".zip".
I add here the link where this piece of code is retrieved, in addition to making a
Explanation of the use of zip and gzip libraries in Java.Click here(Now ask for you to)
register to be able to access the article.
Something I have had trouble with is creating a jar file using these libraries,
I have not found how to make a jar that is recognized when added to
classpath and run the java application, if someone has an example regarding that, I
I would appreciate any comments.
Let's look at a simple example of how to create a zip file using Java. For the
For example, we will have a .txt file with a piece of text and we are going to package it in a
file.zip. We will use the classes provided by Java in itsjava.util.zip package
Once this is done, we just need to start adding files. To do this, the steps to follow are
what to give
The name we use in ZipEntry is the name we want the file to have.
inside the zip. Although it is usual, it does not have to match the name of the
file outside the zip. If we want the compressed file to be inside a
directory in the zip, it will be enough to also put the path where we want it
Now you have to read the normal file and pass the bytes to the ZipOutputStream.
Once we have finished reading and inputting bytes from this file, we close both the
file like the zip entry, indicating to ZipOutputStream that we have finished
with this file.
fis.close();
os.closeEntry();
we repeat the process with all the files we want to continue adding, creating
for each of them a new ZipEntry, writing the file bytes and closing
the ZipEntry.
os.close();
If we already have the zip file and we want to read its content and extract it,
we will use the ZipInputStream class by passing the zip file
This class has methods to query how many entries it has, what they are, etc. In this
example that aims to be simple, we are simply going to walk through and extract
all the entries. To go through the entries
ZipEntry entry;
while (null != (input=zis.getNextEntry()) ){
System.out.println(entrada.getName());
...
}
For each ZipEntry, we can interrogate it to know its size, compression format,
etc, etc. Just look at theZipEntry APIto see the possibilities.
ZipEntry entry;
while (null != (entry=zis.getNextEntry()) ){
System.out.println(entrada.getName());
For each entry, we create a FileOutputStream where we will write the file.
unzipped. Here we have cheerfully put new
FileOutputStream(entrada.getName()), but in reality we must analyze beforehand
the entrada.getName() to see if it is the name of a valid file that we can
write. For example, if as we saw when writing the zip, the name of the entry were
"directory/file.txt", before opening we should create the directory or we will get a
error.
Then, a read buffer is simply declared and we enter a loop until the end.
from file, reading from the ZipInputStream and writing to the FileOutputStream.
After the transfer is finished, we close the FileOutputStream and inform the ZipInputStream
that we have finished with the entry, calling closeEntry().
And once we exit the loop of entries (no more entries remain in the zip),
we close the ZipOutputStream
zip.close();