|
悬赏1金钱已解决
本帖最后由 呵呵仙 于 2024-9-17 19:41 编辑
请问压缩数据()和解压数据(),炫语言有官方命令吗?这个命令在易语言经常用的,炫语言没有不应该吧!
问了下百度AI.嵌入代码后报错了!!不知如何搞!?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <zlib.h>
int compress(Bytef *data, uLong ndata, Bytef *zdata, uLong *nzdata) {
int err;
z_stream c_stream;
printf("Compressing:\n");
c_stream.zalloc = NULL;
c_stream.zfree = NULL;
c_stream.opaque = NULL;
if (deflateInit(&c_stream, Z_DEFAULT_COMPRESSION) != Z_OK) return -1;
c_stream.next_in = data;
c_stream.avail_in = ndata;
c_stream.next_out = zdata;
c_stream.avail_out = *nzdata;
while (c_stream.avail_in != 0 && c_stream.total_out < *nzdata) {
err = deflate(&c_stream, Z_NO_FLUSH);
if (err != Z_OK) return -1;
}
if (err != Z_STREAM_END) return -1;
err = deflateEnd(&c_stream);
if (err != Z_OK) return -1;
*nzdata = c_stream.total_out;
printf("Compressed %ld bytes to %ld bytes\n", ndata, *nzdata);
return 0;
}
int decompress(Byte *zdata, uLong nzdata, Byte *data, uLong *ndata) {
int err;
z_stream d_stream; /* decompression stream */
printf("Decompressing:\n");
d_stream.zalloc = NULL;
d_stream.zfree = NULL;
d_stream.opaque = NULL;
d_stream.next_in = zdata;
d_stream.avail_in = 0;
d_stream.next_out = data;
if ((err = inflateInit(&d_stream)) != Z_OK) return err;
while (d_stream.total_out < *ndata && d_stream.total_in < nzdata) {
d_stream.avail_in = d_stream.avail_out = 1;
if ((err = inflate(&d_stream, Z_NO_FLUSH)) == Z_STREAM_END) break;
if (err != Z_OK) return err;
}
if (err != Z_STREAM_END) return err;
err = inflateEnd(&d_stream);
if (err != Z_OK) return err;
*ndata = d_stream.total_out;
printf("Decompressed %ld bytes to %ld bytes\n", nzdata, *ndata);
return 0;
}
int main() {
char *original = "Hello, this is a test string for compression!";
unsigned long original_length = strlen(original);
unsigned long compressed_length = original_length + (original_length / 10) + 12;
char *compressed = (char *)malloc(compressed_length);
char *decompressed = (char *)malloc(original_length);
compress((Bytef*)original, original_length, (Bytef*)compressed, &compressed_length);
decompress((Byte*)compressed, compressed_length, (Byte*)decompressed, &original_length);
printf("Original: %s\n", original);
printf("Decompressed: %s\n", decompressed);
free(compressed);
free(decompressed);
return 0;
}
|
|