90 lines
2.7 KiB
Java
Raw Normal View History

2010-01-19 00:39:52 -03:00
/*
2012-10-04 20:48:01 -03:00
* Copyright [2012] [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;
import java.io.IOException;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
2010-01-19 00:39:52 -03:00
import java.nio.charset.Charset;
2009-06-07 18:59:43 +00:00
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;
import java.util.zip.ZipEntry;
public class ZipUtils {
public static String zipToString(byte[] zip) throws IOException {
String result = null;
if (zip != null)
{
final ByteArrayInputStream in = new ByteArrayInputStream(zip);
final ZipInputStream zipIn = new ZipInputStream(in);
zipIn.getNextEntry();
byte[] buffer = new byte[512];
int len;
2011-07-03 12:13:31 -03:00
StringBuilder sb_result = new StringBuilder();
2009-06-07 18:59:43 +00:00
while ((len = zipIn.read(buffer)) > 0) {
2010-01-19 00:39:52 -03:00
sb_result.append(new String(buffer, 0, len, Charset.forName("UTF-8")));
2009-06-07 18:59:43 +00:00
}
zipIn.closeEntry();
zipIn.close();
result = sb_result.toString();
}
return result;
}
public static byte[] stringToZip(String content) throws IOException {
ZipOutputStream zip = null;
ByteArrayOutputStream byteArray = new ByteArrayOutputStream();
2010-01-19 00:39:52 -03:00
ByteArrayInputStream in = new ByteArrayInputStream(content.getBytes("UTF-8"));
2009-06-07 18:59:43 +00:00
try {
zip = new ZipOutputStream(byteArray);
ZipEntry zEntry = new ZipEntry("content");
zip.putNextEntry(zEntry);
int bytesRead;
byte[] buffer = new byte[8192];
while ((bytesRead = in.read(buffer, 0, 8192)) != -1) {
zip.write(buffer, 0, bytesRead);
}
zip.closeEntry();
}
finally
{
if (zip != null)
{
zip.flush();
zip.close();
}
}
return byteArray.toByteArray();
}
}