导航:首页 > 文件处理 > byte数组压缩

byte数组压缩

发布时间:2022-10-22 19:58:06

㈠ as3如何传递ByteArray给js

Socket数据获取
/**
* 当客户端给服务端传递信息处理,这里将自动处理内容分割
* @param e
*/
protected function onSocketData(e:ProgressEvent):void
{
if(socket.bytesAvailable == 0)
return;
//将数据储存在缓存区
var byte:ByteArray = new ByteArray();
socket.readBytes(byte,0,e.bytesTotal);
pushCache(byte);
parsingCache();
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
当Socket给我们传输数据的时候,我们可以侦听ProgressEvent事件进行加载读取。Socket的传输原理就跟下载文件一样,你在下载服务端那边的数据,只是数据是自定义的,但数据是按循序而来的。

将数据添加到缓存
/**
* 写入缓存区
* @param byte
*/
public function pushCache(byte:ByteArray):void
{
if(_cache == null)
{
_cache = byte;
}
else
{
_cache.position = _cache.length;
_cache.writeBytes(byte);
byte.clear();
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
下载数据,意味着并不是每次下载的数据都是完整的,可能会有错位,数据不完整的情况。所以需要将下载到的数据,装到一个ByteArray里作为缓存,方便下一次获取使用。

自定义发送内容格式
/**
* 发送信息到服务端,或者客户端
* @param data
*/
public function send(data:Object):void
{
//新建一个二进制内容
var byte:ByteArray = new ByteArray();
//写入Object
byte.writeObject(data);
//压缩二进制内容
byte.compress();
//传输内容长度
socket.writeShort(byte.length);
//传输内容
socket.writeBytes(byte);
//确定内容写入
socket.flush();
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
这里有一个自定义方法send,里面的data就是我要发送的内容,但不管是什么内容,都将它装载到一个ByteArray数组里。重要的是,ByteArray自带有compress的压缩功能,这意味着传输的宽带需求进行了压缩。这里的传输顺序是:内容长度(Short):内容(ByteArray)。

解析接收到的内容
/**
* 解析分发数据包
*/
public function parsingCache():void
{
//判断缓存是否存在
if(!_cache)
return;
//初始化缓存的读取位置
var par:int = 0;
_cache.position = par;
//如果缓存的字节大于2,因为Short占位2个字符
while(_cache.bytesAvailable > 2){
//解析时,读取内容长度,这里会取出2个字符
var len:uint = _cache.readShort();
//判断缓存的可用长度是否足够读取内容
if(_cache.bytesAvailable < len)
{
//当缓存不足读取时,中断读取
break;
}
else
{
//当缓存足够可读取时,将当前的缓存位置增加两个字符,跳过Short内容。
_cache.position = par + 2;
//读取其内容
var data:ByteArray = new ByteArray();
_cache.readBytes(data,0,len);
//返回数据
backData(data);
//缓存位置刷新至下一个
par += 2 + len;
_cache.position = par;
}
}
//判断缓存的位置是否做了刷新,如果有刷新,意味着读取过内容
if(par != 0)
{
//判断缓存的可用长度
if(_cache.bytesAvailable > 0)
{
//存在可用的长度时,截取par后面未使用的新数据,以便下一次使用
var newbyte:ByteArray = new ByteArray();
_cache.position = par;
_cache.readBytes(newbyte,0,_cache.bytesAvailable);
_cache.clear();
_cache = newbyte;
}
else
{
//如果数据本身不存在可用数据,那么可以进行释放
_cache.clear();
_cache = null;
}
}
}

//数据返回,进行解析
private function backData(data:ByteArray):void
{
try{
//还原读取点
data.position = 0;
//解压数据
data.uncompress();
//读取之前定义好的Object内容
var ob:Object = data.readObject();
if(ob)
{
//自定义操作
if(dataFunc != null)
dataFunc(ob);
}
}
catch(e:Error)
{
trace("解析出现异常");
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
这里做了ByteArray的解析处理,根据其内容长度,读取内容。Short占两个字符,因此跳过固定为2值。有一个固定的规则,二进制的内容都是自由定义的。

java ByteArrayOutputStream从压缩文件中读取到字节数组,转换成字符串后再转换成数组发现数据不一样了

您好, 提问者:
ISO8859-1是占1个字节。
而UTF-8的汉字是占三个字节。
GBK的汉字的是占两个字节,当然不一样了。
//转换
new String(splitData.getBytes("ISO8859-1"),"UTF-8");

㈢ byte数组怎么存放到Json中传递

json 是一种很简洁的协议,但可惜的是,它只能传递基本的数型(int,long,string等),但不能传递byte类型。如果想要传输图片等二进制文件的话,是没办法直接传输。
本文提供一种思路给大家参考,让大家可以在json传输二进制文件,如果大家有这个需求又不知怎么实现的话,也许本文能够帮到你。思想适用于所有语言,本文以Java实现,相信大家很容易就能转化为自己懂得语言。

思路
1. 读取二进制文件到内存
2. 用Gzip压缩一下。毕竟是在网络传输嘛,当然你也可以不压缩。
3. 用Base64 把byte[] 转成字符串

㈣ 如何将BYTE数组初始化

文件格式:mysql-noinstall-version-win32.zip
在安装之前,请下载mysql-noinstall-5.1.31-win32.zip和mysql-noinstall-5.0.77-win32.zip这两个文件。

由于MySQL一直在升级,所以当您阅读本文时,或许在其官方网站上已提供了新的正式版下载,因此,请进入MySQL下载页面http://dev.mysql.com/downloads/ 查找当前的最新可用版本。
另外,在下载过程中,会提示你需要注册为网络用户,这是一个很简短的过程,它需要您提供一个有效的email地址。当然,如果不注册的话,你也可以跳过此步,直接下载既可。

2、解压缩并放置到你期望的目录下
(建议放置在不包含空格的目录下,如E:\mysql )

为了方便,修改解压缩后的文件夹名为mysql,并放置在C:\mysql (因为是配置文件里默认的路径。)

将下载后的mysql-noinstall-5.0.77-win32.zip解压到%MySQL_HOME%(此处%MySQL_HOME%表示MySQL的安装路径,如我将其解压至E:\dev\,为了方便,修改解压缩后的文件夹名为mysql-5.1.31)。另外,为了便于日后备份和升级,最好把安装文件中的data目录存放在其它地方。此处,我在E盘新建一个目录"MySQL5.1Data",然后把"E:\dev\mysql-5.1.31"目录下的"data"子目录剪切到"E:\MySQL5.1Data"目录中。
3、创建一个配置文件

对于服务器每次启动都用到的选项和对MySQL服务的一些设置,你会发现使用配置文件来指定MySQL配置非常方便。

MySQL配置文件,一般放在Windows系统目录中,如C:\WINDOWS 或C:\WINNT ,名为my.ini 。但是,此处我们是安装两个MySQL,因此将其放在MySQL解压缩后的文件夹中。在该文件夹中,一般包含5个MySQL自带的配置文件,my-small.ini、my-medium.ini、my-large.ini、my-huge.ini和my-innodb-heavy-4G.ini,请你根据自己机器的内存大小,选择其一,并把它重新命名为my.ini用作基本配置文件。

配置文件中的一些参数,需要根据安装目录的不同,做相应的修改,如
[WinMySQLAdmin]
Server=E:/dev/mysql-5.1.31/bin/mysqld.exe
[mysqld]
basedir=E:/dev/mysql-5.1.31
datadir=E:/MySQL5.1Data/data
default-character-set=gbk
port=3306
[client]
default-character-set=gbk
port=3306
4、将MySQL加入到Windows 的服务中

打开MS-DOS窗口,进入DOS环境,切换到"%MySQL_HOME%\bin"目录
运行:
%MySQL_HOME%\bin>mysqld --install mysql5.1
(此时,在运行中输入"services.msc"或者打开"控制面板"->"管理工具"->"服务",可以看到服务列表中存在"MySQL5.1"服务。如果不希望它自动运行,也可以改为手动,这样,当你不用它的时候,也可以节省内存。)
5、启动和停止MySQL服务
%MySQL_HOME%\bin>net start mysql
或者进入DOS环境,进入如下目录运行:%MySQL_HOME%\bin\mysqld.exe
(或直接双击mysqld.exe文件),即可启动MySQL服务

%MySQL_HOME%\bin>net stop mysql(停止MySQL服务)
注:如果要卸载MySQL服务,请运行%MySQL_HOME%\bin>mysqld --remove
(如果你不再需要MySQL了,请先停掉MySQL的服务,然后再把MySQL的服务卸载掉,最后删除MySQL安装目录即可。)
6、进入MySQL。

%MySQL_HOME%\bin\mysql -uroot -p (密码为空)

(如果出现ERROR 2003 (HY000): Can't connect to MySQL server on 'localhost' (10061)提示,表示MySQL服务没有被启动,其主要的原因在于,配置文件写的不对。

如果出现ERROR 1045 (28000): Access denied for user [email=]'root'@'localhost'[/email] (using password: NO)提示,表示MySQL服务已经启动,但是你所输入的密码不正确。
如果出现1067的错误:A system error has occurred.System error 1067 has occurred.The process terminated unexpectedly.提示,无法启动MySQL服务,进程意外终止,表示你的配置文件有问题,请检查my.ini里面的路劲是否写错了或者试着删除MySQL5.1Data文件中的LOG文件。)

mysql>select version(); (显示目前你所安装的MySQL的数据库版本信息)
如果上面的所有操作步骤均正常的话,那么恭喜你,你已安装好了MySQL 5.1,下面我们继续安装MySQL 5.0.
(二)安装MySQL 5.0
安装MySQL 5.0的步骤与安装MySQL 5.1的步骤类似,其中,有两个需要注意的地方为,
(1)在创建MySQL 5.0的配置文件时,port要设置成非3306(如3307等)端口。
(2)将MySQL加入到Windows 的服务中:

打开MS-DOS窗口,进入DOS环境,切换到"%MySQL_HOME%\bin"目录
运行:

%MySQL_HOME%\bin>mysqld-nt --install mysql5.0
注意:mysqld(或mysqld.exe)是 MySQL server数据库服务器的相关程序,而mysqld-nt(或mysqld-nt.exe)是MySQL Daemon数据库服务的相关程序,在MySQL 5.1(此处的版本为5.1.31)中仅有mysqld(或mysqld.exe),在MySQL 5.0(此处的版本为5.0.77)中,这两者都有

android用ZipOutputStream压缩byte数组怎么压缩呀求代码!在线等,急!!!

OutputStream os = ...
byte[] bytes = ...
GZIPOutputStream zos = new GZIPOutputStream(new BufferedOutputStream(os));
try {
zos.write(bytes);
} finally {
zos.close();
}

android API 文档中,都有说明

㈥ C# 用7z压缩一组byte[] 然后对这个byte[]进行解压。

usingSystem;
usingSystem.IO;
usingSystem.Text;
usingSevenZip;
classProgram
{
staticbyte[]LzmaCompress(byte[]inpbuf)
{
CoderPropID[]propIDs=
{
CoderPropID.DictionarySize,
CoderPropID.PosStateBits,
CoderPropID.LitContextBits,
CoderPropID.LitPosBits,
CoderPropID.Algorithm,
CoderPropID.NumFastBytes,
CoderPropID.MatchFinder,
CoderPropID.EndMarker
};
object[]properties=
{
(Int32)(23),
(Int32)(2),
(Int32)(3),
(Int32)(2),
(Int32)(1),
(Int32)(128),
(string)("bt4"),
(bool)(true)
};
varenc=newSevenZip.Compression.LZMA.Encoder();
enc.SetCoderProperties(propIDs,properties);

MemoryStreammsInp=newMemoryStream(inpbuf);
MemoryStreammsOut=newMemoryStream();
enc.WriteCoderProperties(msOut);
enc.Code(msInp,msOut,-1,-1,null);
returnmsOut.ToArray();
}
staticbyte[]LzmaDecompress(byte[]inpbuf)
{
CoderPropID[]propIDs=
{
CoderPropID.DictionarySize,
CoderPropID.PosStateBits,
CoderPropID.LitContextBits,
CoderPropID.LitPosBits,
CoderPropID.Algorithm,
CoderPropID.NumFastBytes,
CoderPropID.MatchFinder,
CoderPropID.EndMarker
};
object[]properties=
{
(Int32)(23),
(Int32)(2),
(Int32)(3),
(Int32)(2),
(Int32)(1),
(Int32)(128),
(string)("bt4"),
(Int32)(0)
};
vardec=newSevenZip.Compression.LZMA.Decoder();
byte[]prop=newbyte[5];
Array.Copy(inpbuf,prop,5);
dec.SetDecoderProperties(prop);
MemoryStreammsInp=newMemoryStream(inpbuf);
msInp.Seek(5,SeekOrigin.Current);
MemoryStreammsOut=newMemoryStream();
dec.Code(msInp,msOut,-1,-1,null);
returnmsOut.ToArray();
}
staticvoidMain(string[]args)
{
stringtoEnc="kjasdhfkajgj长一点长一点长一点点点点点lrejfoijoigjeoi啊啊啊啊哇哇哇哇啦啦啦asdasfamovijro竟敢浪费看adslf发送端发送端看啦的发散烦";
byte[]pToEnd=Encoding.UTF32.GetBytes(toEnc);
Console.WriteLine("压缩前{0}个字节",pToEnd.Length);
byte[]a=LzmaCompress(pToEnd);
Console.WriteLine("压缩后变为{0}个字节",a.Length);
byte[]b=LzmaDecompress(a);
stringafterDec=Encoding.UTF32.GetString(b);
if(afterDec==toEnc)
Console.WriteLine("解压成功");
return;
}
}

DLL是通过它提供的SDK里的代码编译来的。lzmasdk下载来后里面有个文件夹叫cs,就是那里面的代码编译来的。工程里添加DLL引用会吧?

㈦ 用 javax.imageio 实现图片压缩原理是什么

下面是传进byte[],传出byte[]数组的,稍微修改即可即可,(data为传过来的byte[]数组)
Java代码
ByteArrayInputStream is = new ByteArrayInputStream(data);

BufferedImage src = null;
ByteArrayOutputStream out = null;
ImageWriter imgWrier;
ImageWriteParam imgWriteParams;

// 指定写图片的方式为 jpg
imgWrier = ImageIO.getImageWritersByFormatName("jpg").next();
imgWriteParams = new javax.imageio.plugins.jpeg.JPEGImageWriteParam(null);

// 要使用压缩,必须指定压缩方式为MODE_EXPLICIT
imgWriteParams.setCompressionMode(imgWriteParams.MODE_EXPLICIT);
// 这里指定压缩的程度,参数qality是取值0~1范围内,
imgWriteParams.setCompressionQuality((float)0.1/data.length);

imgWriteParams.setProgressiveMode(imgWriteParams.MODE_DISABLED);
ColorModel colorModel = ColorModel.getRGBdefault();
// 指定压缩时使用的色彩模式
imgWriteParams.setDestinationType(new javax.imageio.ImageTypeSpecifier(colorModel, colorModel
.createCompatibleSampleModel(16, 16)));

try {
src = ImageIO.read(is);
out = new ByteArrayOutputStream(data.length);

imgWrier.reset();
// 必须先指定 out值,才能调用write方法, ImageOutputStream可以通过任何 OutputStream构造
imgWrier.setOutput(ImageIO.createImageOutputStream(out));
// 调用write方法,就可以向输入流写图片
imgWrier.write(null, new IIOImage(src, null, null), imgWriteParams);

out.flush();
out.close();
is.close();
data = out.toByteArray();
} catch(Exception e) {
e.printStackTrace();
}

㈧ C#实现 压缩文件 总是有这句 byte[] bytes = new byte[40960]; 后面的参数传的什么意思,

byte[] bytes = new byte[40960];
这种字节数组通常视为内存块,做缓存用。后面参数?指40960吗。
代表这块内存大约40KB左右

出现“如果传的参数小 就不能压缩比 参数大的 文件了?”
说明代码被写成先把整个文件读入缓存。缓存小了,就不能压。
说明设计写法错误
通常缓存长度不需要太大,使用正确时可以用4K以下缓存压缩任一长度数据。。

㈨ java编程:数据压缩规则

你是不是问如何编写一个程序来压缩数据?如果你是想解决这个问题的话,继续往下看,如果你只是想要应付考试的话就算了。

—— 分割线

JDK本身提供了数据压缩的一个API

java.util.zip.Deflater.deflateBytes(byte[] b, int off, int len)

以下是我使用的一个例子,有点多,注释看不懂可以问我,不知道怎么用可以问我,其他的就算了。

/**
* threshold value for compress
*/
public static final int THRESHOLD = 1200;

/**
* Answer a byte array compressed in the DEFLATER format from bytes.
*
* @param bytes
* a byte array
* @return byte[] compressed bytes
* @throws IOException
*/
public static byte[] compress(byte[] bytes)
{
// Create the compressor with highest level of compression
Deflater compressor = new Deflater();
compressor.setLevel(Deflater.BEST_COMPRESSION);

// Give the compressor the data to compress
compressor.setInput(bytes);
compressor.finish();

// Create an expandable byte array to hold the compressed data.
// You cannot use an array that's the same size as the orginal because
// there is no guarantee that the compressed data will be smaller than
// the uncompressed data.
ByteArrayOutputStream bos = new ByteArrayOutputStream(bytes.length);

// Compress the data
byte[] buf = new byte[1024];
while (!compressor.finished())
{
int count = compressor.deflate(buf);
bos.write(buf, 0, count);
}
try
{
bos.close();
}
catch (IOException e)
{
}

// Get the compressed data
byte[] compressedData = bos.toByteArray();
return compressedData;
}

/**
* Answer a byte array that has been decompressed from the DEFLATER format.
*
* @param bytes
* a byte array
* @return byte[] compressed bytes
* @throws IOException
*/
public static byte[] uncompress(byte[] bytes)
{
// Create the decompressor and give it the data to compress
Inflater decompressor = new Inflater();
decompressor.setInput(bytes);

// Create an expandable byte array to hold the decompressed data
ByteArrayOutputStream bos = new ByteArrayOutputStream(bytes.length);

// Decompress the data
byte[] buf = new byte[1024];
while (!decompressor.finished())
{
try
{
int count = decompressor.inflate(buf);
bos.write(buf, 0, count);
}
catch (DataFormatException e)
{
}
}
try
{
bos.close();
}
catch (IOException e)
{
}

// Get the decompressed data
byte[] decompressedData = bos.toByteArray();
return decompressedData;
}

㈩ 请问C语言中大数组如何处理

你仔细看看这些数据都是有规律的,有相当大的一部分都是0 1 2 3 4 5 6 13重复了很多次。统计一下这些循环出现了多少次,如果考虑这个因素,压缩还是可以的。
否则的话,所有的数据都是0-15区间的整数,即使考虑使用4位二进制压缩,1个字节(8 bit)可以存2个整数,41502个数也需要20K byte,已经超过内存上限了...
如果数据没有规律,而且实在要压缩的话,考虑用霍夫曼编码可能好一些,不过我估计也挺悬的。可以考虑其他方式,不要保存在内存中,程序运行时需要哪一部分数据再读进来。

阅读全文

与byte数组压缩相关的资料

热点内容
文件夹侧面目录标签怎么制作 浏览:230
做程序员学什么 浏览:320
pdfeditor教程 浏览:880
fortran把文件放入文件夹 浏览:709
程序员1年经验不敢投简历 浏览:481
如何看电脑的源码 浏览:897
找工作app软件哪个好 浏览:96
信息管理网站源码 浏览:439
小说app哪个好免费 浏览:224
域名在线加密 浏览:146
软件编程西安交大 浏览:453
是不是串货的奶粉查不到溯源码的 浏览:825
北京dns服务器云主机 浏览:221
openldaplinux安装 浏览:23
java取月的最后一天 浏览:10
腾讯云服务器多久退款 浏览:949
微信广告植入系统源码 浏览:922
一年级语文上册pdf 浏览:315
好久不见app干什么用的 浏览:143
压缩包解压码对方可以更改吗 浏览:256