69 lines
2.2 KiB
Java
Raw Normal View History

2010-01-19 00:39:52 -03:00
/*
2015-04-12 00:15:12 -03:00
* Copyright [2015] [wisemapping]
2010-01-19 00:39:52 -03:00
*
2011-01-23 21:03:12 -03:00
* Licensed under WiseMapping Public License, Version 1.0 (the "License").
* It is basically the Apache License, Version 2.0 (the "License") plus the
* "powered by wisemapping" text requirement on every single page;
* you may not use this file except in compliance with the License.
* You may obtain a copy of the license at
2010-01-19 00:39:52 -03:00
*
* http://www.wisemapping.org/license
2010-01-19 00:39:52 -03:00
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
2010-01-19 00:39:52 -03:00
*/
2009-06-07 18:59:43 +00:00
package com.wisemapping.util;
2012-11-19 22:47:20 -03:00
import org.apache.commons.io.IOUtils;
import org.jetbrains.annotations.NotNull;
2009-06-07 18:59:43 +00:00
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
2012-11-19 22:47:20 -03:00
import java.io.IOException;
import java.util.zip.ZipEntry;
2009-06-07 18:59:43 +00:00
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;
public class ZipUtils {
2012-11-19 22:47:20 -03:00
public static byte[] zipToBytes(byte[] zip) throws IOException {
2009-06-07 18:59:43 +00:00
2012-11-19 22:47:20 -03:00
byte[] result = null;
if (zip != null) {
2009-06-07 18:59:43 +00:00
final ByteArrayInputStream in = new ByteArrayInputStream(zip);
final ZipInputStream zipIn = new ZipInputStream(in);
zipIn.getNextEntry();
2012-11-19 22:47:20 -03:00
result = IOUtils.toByteArray(zipIn);
2009-06-07 18:59:43 +00:00
zipIn.closeEntry();
zipIn.close();
}
return result;
}
2012-11-19 22:47:20 -03:00
public static byte[] bytesToZip(@NotNull final byte[] content) throws IOException {
2009-06-07 18:59:43 +00:00
ZipOutputStream zip = null;
2012-11-19 22:47:20 -03:00
final ByteArrayOutputStream byteArray = new ByteArrayOutputStream();
2009-06-07 18:59:43 +00:00
try {
zip = new ZipOutputStream(byteArray);
ZipEntry zEntry = new ZipEntry("content");
zip.putNextEntry(zEntry);
2012-11-19 22:47:20 -03:00
IOUtils.write(content, zip);
2009-06-07 18:59:43 +00:00
zip.closeEntry();
2012-11-19 22:47:20 -03:00
} finally {
if (zip != null) {
2009-06-07 18:59:43 +00:00
zip.flush();
zip.close();
}
}
return byteArray.toByteArray();
}
}