Home
|
Tutorials
|
Articles
|
Videos
|
Products
|
Tools
|
Search
Interviews
|
Open Source
|
Tag Cloud
|
Follow Us
|
Bookmark
|
Contact
In Browser
StumbleUpon
del.icio.us
Google
Google Buzz
reddit
LinkedIn
Facebook
Twitter
Linkedin
E-Mail
Home
Tutorials
Articles
Search Tutorials
Products
Authors
Submit a Tutorial
Report a Bug
Interview FAQ
Subscribe
Articles
>
Misc
> How to create Zip stream/file content in-memory
How to create Zip stream/file content in-memory
Author:
Venkata Sudhakar
The below example shows how to create a Zip stream/file content in-memory.
package com.bethecoder.utils; import java.io.ByteArrayOutputStream; import java.io.File; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; import org.apache.commons.io.FileUtils; public class ZipDirectory { public static void main(String[] args) { byte [] zipContent = createZipByteArray(new File("C:\\DIR_TO_ZIP")); } public static byte[] createZipByteArray(File directoryToZip) { byte[] zipContent = new byte[0]; try { ByteArrayOutputStream output = new ByteArrayOutputStream(); ZipOutputStream zipOut = new ZipOutputStream(output); for (File fileToZip : directoryToZip.listFiles()) { if (fileToZip.isFile()) { ZipEntry zipEntry = new ZipEntry(fileToZip.getName()); zipOut.putNextEntry(zipEntry); zipOut.write(FileUtils.readFileToByteArray(fileToZip)); } } zipOut.close(); zipContent = output.toByteArray(); } catch (Exception e) { e.printStackTrace(); } return zipContent; } }
Home
|
Tutorials
|
Random Tutorial
|
Articles
|
Videos
|
Search
|
Google Search
|
Products
|
Student Projects
|
Open Source
Tools
|
Tips And Tricks
|
How to Fix
|
Tag Cloud
|
Share
|
Disclaimer
|
Privacy
|
Contact
|
Login
Send your comments, suggestions or queries regarding this site to
[email protected]
.
Copyright © 2008-2024. All rights reserved.