急求lzw算法的英文文本压缩C语言源代码!

急求lzw算法的英文文本压缩C语言源代码!,第1张

#include<iostream>

#include<cstdio>

#include<cstring>

#include<ctime>//用来计算压缩的时间

using namespace std

//定义常数

const int MAX = 1000003//最大code数,是一个素数,求模是速度比较快

const int ascii = 256 //ascii代码的数量

const int ByteSize = 8//8个字节

struct Element//hash表中的元素

{

int key

int code

Element *next

}*table[MAX]//hash表

int hashfunction(int key)//hash函数

{

return key%MAX

}

void hashinit(void)//hash表初始化

{

memset(table,0,sizeof(table))

}

void hashinsert(Element element)//hash表的插入

{

int k = hashfunction(element.key)

if(table[k]!=NULL)

{

Element *e=table[k]

while(e->next!=NULL)

{

e=e->next

}

e->next=new Element

e=e->next

e->key = element.key

e->code = element.code

e->next = NULL

}

else

{

table[k]=new Element

table[k]->key = element.key

table[k]->code = element.code

table[k]->next = NULL

}

}

bool hashfind(int key,Element &element)//hash表的查找

{

int k = hashfunction(key)

if(table[k]!=NULL)

{

Element *e=table[k]

while(e!=NULL)

{

if(e->key == key)

{

element.key = e->key

element.code = e->code

return true

}

e=e->next

}

return false

}

else

{

return false

}

}

void compress(void)//压缩程序

{

//打开一个流供写入

FILE *fp

fp = fopen("result.dat", "wb")

Element element

int used

char c

int pcode, k

for(int i=0i<asciii++)

{

element.key = i

element.code = i

hashinsert(element)

}

used = ascii

c = getchar()

pcode = c

while((c = getchar()) != EOF)

{

k = (pcode <<ByteSize) + c

if(hashfind(k, element))

pcode = element.code

else

{

//cout<<pcode<<' '

fwrite(&pcode, sizeof(pcode), 1, fp)

element.code = used++

element.key = (pcode <<ByteSize) | c

hashinsert(element)

pcode = c

}

}

//cout<<pcode<<endl

fwrite(&pcode, sizeof(pcode), 1, fp)

}

int main(void)

{

int t1,t2

//欲压缩的文本文件

//freopen("input.txt","r",stdin)

freopen("book5.txt","r",stdin)

t1=time(NULL)

hashinit()

compress()

t2=time(NULL)

cout<<"Compress complete! See result.dat."<<endl

cout<<endl<<"Total use "<<t2-t1<<" seconds."<<endl

package com.io2.homework

import java.io.File

import java.io.FileInputStream

import java.io.FileNotFoundException

import java.io.FileOutputStream

import java.io.IOException

import java.util.zip.ZipEntry

import java.util.zip.ZipOutputStream

/*压缩文件夹*/

public class MyMultipleFileZip

{

private String currentZipFilePath = "F:/MyZip.zip"

private String sourceFilePath

private ZipOutputStream zos

private FileInputStream fis

public MyMultipleFileZip(String sourceFilePath)

{

try

{

this.sourceFilePath = sourceFilePath

zos = new ZipOutputStream(new FileOutputStream(currentZipFilePath))

//设定文件压缩级别

zos.setLevel(9)

} catch (FileNotFoundException e)

{

e.printStackTrace()

}

}

// 在当前条目中写入具体内容

public void writeToEntryZip(String filePath)

{

try

{

fis = new FileInputStream(filePath)

} catch (FileNotFoundException e1)

{

e1.printStackTrace()

}

byte[] buff = new byte[1024]

int len = 0

try

{

while ((len = fis.read(buff)) != -1)

{

zos.write(buff, 0, len)

}

} catch (IOException e)

{

e.printStackTrace()

}finally

{

if (fis != null)

try

{

fis.close()

} catch (IOException e)

{

e.printStackTrace()

}

}

}

// 添加文件条目

public void addFileEntryZip(String fileName)

{

try

{

zos.putNextEntry(new ZipEntry(fileName))

} catch (IOException e)

{

e.printStackTrace()

}

}

public void addDirectoryEntryZip(String directoryName)

{

try

{

zos.putNextEntry(new ZipEntry(directoryName + "/"))

} catch (IOException e)

{

e.printStackTrace()

}

}

// 遍历文件夹

public void listMyDirectory(String filePath)

{

File f = new File(filePath)

File[] files = f.listFiles()

if(files!=null)

{

for (File currentFile : files)

{

// 设置条目名称(此步骤非常关键)

String entryName= currentFile.getAbsolutePath().split(":")[1].substring(1)

// 获取文件物理路径

String absolutePath = currentFile.getAbsolutePath()

if (currentFile.isDirectory())

{

addDirectoryEntryZip(entryName)

//进行递归调用

listMyDirectory(absolutePath)

}

else

{

addFileEntryZip(entryName)

writeToEntryZip(absolutePath)

}

}

}

}

// 主要流程

public void mainWorkFlow()

{

listMyDirectory(this.sourceFilePath)

if(zos!=null)

try

{

zos.close()

} catch (IOException e)

{

e.printStackTrace()

}

}

public static void main(String[] args)

{

new MyMultipleFileZip("F:/fountainDirectory").mainWorkFlow()

}

}

此例子中用到的控件是 (按钮1)(按钮2)(zip压缩1)

例子是将运行目录下的 “1.ini” 压缩成.ZIP文件,再讲ZIP文件解压到文件夹

代码如下:(效果如  例子图)

.版本 2

.支持库 eCompress

.子程序 _按钮1_被单击

ZIP压缩1.压缩 (取运行目录 () + “\1.ini”, “压缩文件.zip”)

.子程序 _按钮2_被单击

ZIP压缩1.解压 (取运行目录 () + “\压缩文件.zip”, “解压开的文件夹”)


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/yw/11304048.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-15
下一篇 2023-05-15

发表评论

登录后才能评论

评论列表(0条)

保存