How to zipped a file or directory in java
Tech-Today

How to zipped a file or directory in java


There are times when we have to zipped a file or directory recursively in Java. This is how we do it:

public static void zipFile(File file) throws IOException {
byte[] buffer = new byte[1024];

FileOutputStream fos = new FileOutputStream(
file.getParent() + File.separator + FilenameUtils.removeExtension(file.getName()) + ".zip");
ZipOutputStream zos = new ZipOutputStream(fos);
ZipEntry ze = new ZipEntry(file.getName());
zos.putNextEntry(ze);
FileInputStream in = new FileInputStream(file);

int len;
while ((len = in.read(buffer)) > 0) {
zos.write(buffer, 0, len);
}

in.close();
zos.closeEntry();

// remember close it
zos.close();
}

And now this is how we zipped a folder recursively:

public static void zipDir(File file) throws IOException {
FileOutputStream fos = new FileOutputStream(new File(FilenameUtils.removeExtension(file.getParent() + File.separator + file.getName()) + ".zip"));
ZipOutputStream zos = new ZipOutputStream(fos);
FileUtils.addDirToArchive(getRootDir(), file.getPath(), zos);
fos.flush();
zos.close();
fos.close();
}

public static void addDirToArchive(String relativeRoot, String dir2zip, ZipOutputStream zos) throws IOException {
File zipDir = new File(dir2zip);
String[] dirList = zipDir.list();
byte[] readBuffer = new byte[2156];
int bytesIn = 0;

for (int i = 0; i < dirList.length; i++) {
File f = new File(zipDir, dirList[i]);
if (f.isDirectory()) {
String filePath = f.getPath();
addDirToArchive(relativeRoot, filePath, zos);
continue;
}

FileInputStream fis = new FileInputStream(f);
String relativePath = Paths.get(relativeRoot).relativize(f.toPath()).toString();
ZipEntry anEntry = new ZipEntry(relativePath);
zos.putNextEntry(anEntry);

while ((bytesIn = fis.read(readBuffer)) != -1) {
zos.write(readBuffer, 0, bytesIn);
}

fis.close();
}
}

*FilenameUtils is from apache commons




- Encode Image And Display In Html
To encode an image file(eg. png, jpg, gif) we will need the apache commons-codec library. To avoid it in a maven project: <dependency> <groupId>commons-codec</groupId> <artifactId>commons-codec</artifactId> <version>1.11</version>...

- How To Encrypt And Decrypt An Object In And From A File In Java
There are times when we need to write something on a file, but doesn't want it to be readable as a plain text. In this case we can use any type of encryption mechanism, but what if we want to decrypt the encrypted file back and read its contents....

- How To Download A File In Soap Api
First we define the interface like this: @WebMethod ActionStatus downloadFile(@WebParam(name = "file") String file); Implementation: public ActionStatus downloadFile(String filePath) { MessageContext mc = webServiceContext.getMessageContext(); // see...

- How To Download A File Using Rest Api
Normally I create an interface when defining a REST api, this is useful when testing using Arquillian. To define a download file api we must define the method below in the interface. @GET @Path("/downloadFile") ActionStatus downloadFile(@QueryParam("file")...

- How To Download A File From The Server Using Javaee
The following snippet will accept a filename in the server's directory, set it as the content of the FacesContext for the user to download. public String downloadXMLInvoice(String fileName) { File file = new File(getXmlInvoiceDir().getAbsolutePath()...



Tech-Today








.