PHP有办法实现这种java的压缩和解压缩吗?amani11进?

周口店的程序猿 2011-02-14 09:47:25
//压缩
public static String Compress(String data) {
try {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
DeflaterOutputStream zos = new DeflaterOutputStream(bos);
zos.write(data.getBytes());
zos.close();
return new String(new BASE64Encoder().encode(bos.toByteArray())).replaceAll("\r", "").replaceAll("\n", "");
} catch (Exception ex) {
ex.printStackTrace();
return "压缩失败";
}
}
//解压缩
public static String DeCompress(String encdata) {
try {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
InflaterOutputStream zos = new InflaterOutputStream(bos);
zos.write(new BASE64Decoder().decodeBuffer(encdata));
zos.close();
return new String(bos.toByteArray());
} catch (Exception ex) {
ex.printStackTrace();
return "解压缩失败";
}
}

PHP有办法实现这种java的压缩和解压缩吗

amani11在吗?在下面这个帖子里你好像解决过这个问题?
http://topic.csdn.net/u/20100913/14/df810f6f-1b98-4b4e-b689-e083bdae4f5c.html

或者有哪个兄弟知道怎么解决请告知,谢谢。
...全文
376 11 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
额. 谢谢各位了 用gzcompress和gzuncompress解决了
和java的DeflaterOutputStream压缩算法一样!
ihefe 2011-02-14
  • 打赏
  • 举报
回复
你的应该是编码成base64

用这2个函数看看

base64_decode($data)//解
base64_encode($data)//编
  • 打赏
  • 举报
回复
没人?
  • 打赏
  • 举报
回复
我要压缩文本 而非文件
life169 2011-02-14
  • 打赏
  • 举报
回复
PclZip官方地址 :http://www.phpconcept.net/pclzip/index.php
PclZip手册地址 :http://www.phpconcept.net/pclzip/man/en/index.php
PEAR类创建 ZIP档案文件 :http://www.ccvita.com/10.html
PclZip简介与使用 :http://www.ccvita.com/59.html
PclZip:强大的PHP压缩与解压缩zip类 :http://www.ccvita.com/330.html
life169 2011-02-14
  • 打赏
  • 举报
回复
PclZip强大的PHP压缩与解压缩zip类

如何使用PclZip


1.基础
所有的功能都由pclzip.lib.php这个档案提供,PclZip library可于其首页(www.phpconcept.net/pclzip/index.en.php)下载。所有的PKZIP档案其实就是一个 PclZip的类别物件。当产生一个PclZip档案(ie, PclZip类别物件),就会先产生一个压缩档,且档名已经指定,但此压缩档的内容尚未存在:

<?PHP
require_once('pclzip.lib.php');
$archive = new PclZip('archive.zip');

$v_list = $archive->add('dev/file.txt',
PCLZIP_OPT_REMOVE_PATH, 'dev');
?>
[/code]
上面的就是把file.txt 压缩成:archive.zip
  • 打赏
  • 举报
回复
求amani11 的 QQ 呵呵
skyaspnet 2011-02-14
  • 打赏
  • 举报
回复
[Quote=引用楼主 shiningsb 的回复:]
//压缩
public static String Compress(String data) {
try {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
DeflaterOutputStream zos = new DeflaterOutputStream(bos);
zos.write(data.getBytes(……
[/Quote]

文本压缩使用 gzencode和gzdecode,可以实现字符串的gzip压缩和解压,也就是java同样的压缩效果

gzencode
(PHP 4 >= 4.0.4, PHP 5)

gzencode — Create a gzip compressed string

说明
string gzencode ( string $data [, int $level = -1 [, int $encoding_mode = FORCE_GZIP ]] )
This function returns a compressed version of the input data compatible with the output of the gzip program.

For more information on the GZIP file format, see the document: » GZIP file format specification version 4.3 (RFC 1952).

参数

data
The data to encode.

level
The level of compression. Can be given as 0 for no compression up to 9 for maximum compression. If not given, the default compression level will be the default compression level of the zlib library.

encoding_mode
The encoding mode. Can be FORCE_GZIP (the default) or FORCE_DEFLATE.

If you use FORCE_DEFLATE, you get a standard zlib deflated string (inclusive zlib headers) after the gzip file header but without the trailing crc32 checksum.


返回值
The encoded string, or FALSE if an error occurred.

更新日志
版本 说明
4.2.0 The encoding_mode parameter was added


范例
The resulting data contains the appropriate headers and data structure to make a standard .gz file, e.g.:

Example #1 Creating a gzip file

<?php
$data = implode("", file("bigfile.txt"));
$gzdata = gzencode($data, 9);
$fp = fopen("bigfile.txt.gz", "w");
fwrite($fp, $gzdata);
fclose($fp);
?>

参见

gzdecode() - Decodes a gzip compressed string
gzdeflate() - Deflate a string
gzinflate() - Inflate a deflated string
gzuncompress() - Uncompress a compressed string
gzcompress() - Compress a string

================================================================================

gzdecode
(No version information available, might only be in SVN)

gzdecode — Decodes a gzip compressed string

说明
string gzdecode ( string $data [, int $length ] )
This function returns a decoded version of the input data.

参数

data
The data to decode, encoded by gzencode().

length
The maximum length of data to decode.


返回值
The decoded string, or FALSE if an error occurred.

参见

gzencode() - Create a gzip compressed string

  • 打赏
  • 举报
回复
[Quote=引用 7 楼 wisword 的回复:]

引用 4 楼 shiningsb 的回复:

我要压缩文本 而非文件

compress()
decompress()
[/Quote]

??
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 ihefe 的回复:]

你的应该是编码成base64

用这2个函数看看

base64_decode($data)//解
base64_encode($data)//编
[/Quote]

关键还有一个DeflaterOutputStream的压缩算法
wisword 2011-02-14
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 shiningsb 的回复:]

我要压缩文本 而非文件
[/Quote]
compress()
decompress()

21,893

社区成员

发帖
与我相关
我的任务
社区描述
从PHP安装配置,PHP入门,PHP基础到PHP应用
社区管理员
  • 基础编程社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧