Android Java压缩Zlib,Gzip,Zip支持J2ME
2010-11-05 00:54:54 来源:WEB开发网*
* @param data
* @return
* @throws IOException
*/
public static byte[] zip(byte[] bContent) throws IOException
{
byte[] b = null;
try
{
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ZipOutputStream zip = new ZipOutputStream(bos);
ZipEntry entry = new ZipEntry("zip");
entry.setSize(bContent.length);
zip.putNextEntry(entry);
zip.write(bContent);
zip.closeEntry();
zip.close();
b = bos.toByteArray();
bos.close();
}
catch (Exception ex)
{
ex.printStackTrace();
}
return b;
}
/***
* 解压Zip
*
* @param data
* @return
* @throws IOException
*/
public static byte[] unZip(byte[] bContent) throws IOException
{
byte[] b = null;
try
{
ByteArrayInputStream bis = new ByteArrayInputStream(bContent);
ZipInputStream zip = new ZipInputStream(bis);
while (zip.getNextEntry() != null)
{
byte[] buf = new byte[1024];
int num = -1;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
while ((num = zip.read(buf, 0, buf.length)) != -1)
{
baos.write(buf, 0, num);
}
b = baos.toByteArray();
baos.flush();
baos.close();
}
zip.close();
bis.close();
}
catch (Exception ex)
{
ex.printStackTrace();
}
return b;
}
public static void main(String[] args)
{
String newContent = "";
try
{
String content =
更多精彩
赞助商链接