BigBastis Blog

Java: Create ZIP archive

Introduction

user

Sebastian Gross

Sebastian Gross arbeitet in Bielefeld als Softwareentwickler für .NET und Java im Bereich Web.Als Fan der .NET-Plattform lässt er sich kein Userguppen Treffen und Community Event im Raum OWL entgehen.Dabei hat er eine besondere Vorliebe für das ASP.NET MVC Framework und für das Test Driven Development (TDD) entwickelt.


LATEST POSTS

Handling too long scrollspy menus 10th June, 2015

Using Google Open Auth Login in Asp.Net MVC Applications 26th February, 2015

Java

Java: Create ZIP archive

Posted on .

Introduction

On a website project at work i had to implement a feature that allowed the user to download one or more messages as a zip file. When i started, i really was shocked how complicated it is to zip or unzip a String in java. If you glance over to csharp the whole zip-thing looks easy as pie:

string startPath = @"c:examplestart";
string zipPath = @"c:exampleresult.zip";
string extractPath = @"c:exampleextract";

ZipFile.CreateFromDirectory(startPath, zipPath);

ZipFile.ExtractToDirectory(zipPath, extractPath);

Those wrapper classes from System.IO make it really easy to create and read archives. After a bit of trial and error i was able to create some easy to use java methods which do the same. So for you (and for me as a future reference), here’s the code.

Zipping / Unzipping a String

String -> ZIP

Lets have alook at the usual case where you need to zip a String.

public static byte[] zip(String s) throws IOException {
    Deflater compressor = new Deflater();
    ByteArrayOutputStream boas = new ByteArrayOutputStream();
    try {
        compressor.setInput(s.getBytes("UTF8"));
        compressor.finish();
        byte[] buf = new byte[1024];
        while (!compressor.finished()) {
            int count = compressor.deflate(buf);
            boas.write(buf, 0, count);
        }
    } finally {
        compressor.end();
    }
    return boas.toByteArray();
}

ZIP -> String



404 Not Found

Error: Not Found

The requested URL / was not found on this server.

The code is really straight forward, but it looks ugly and you will possibly just copy & paste it into your project like almost the rest of the world, so why not create an easy to use API (I’m looking at you, Oracle ಠ_ಠ)?

Zipping / Unzipping several files

List -> ZIP

Sometimes you want to create a ZIP file just to combine several files to one archive as a usability feature to the users of your application.



404 Not Found

Error: Not Found

The requested URL / was not found on this server.

The Map parameter carries the Strings you need to be zipped. The first String is the filename that should appear inside the ZIP file and the second String is the content of that file.

ZIP -> List



404 Not Found

Error: Not Found

The requested URL / was not found on this server.

Same here, you provide an archive and get a Map with the files it contained. Again, the first String is the filename and the second String is the file content.

Conclusion

As you can see the whole thing is not really complicated but you wouldn’t be able to write the code without googling. Well, that’s what APIs are for, so lets hope, that in near future there will be a more pleasant way to zip and unzip files in java.

You can have a look at the whole class file in my github repo.

Happy coding!

profile

Sebastian Gross

http://www.bigbasti.com

Sebastian Gross arbeitet in Bielefeld als Softwareentwickler für .NET und Java im Bereich Web.Als Fan der .NET-Plattform lässt er sich kein Userguppen Treffen und Community Event im Raum OWL entgehen.Dabei hat er eine besondere Vorliebe für das ASP.NET MVC Framework und für das Test Driven Development (TDD) entwickelt.

There are no comments.

Kommentar verfassen

View Comments (0) ...
Navigation