「java精度压缩」Java压缩
今天给各位分享java精度压缩的知识,其中也会对Java压缩进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!
本文目录一览:
Java数据压缩格式程序设计方法
GZIP压缩格式简介在JDK API中 同样定义了多种类型用于创建和解除GZIP压缩格式数据文件的通用对象和方法 用于基于JDK编写GZIP压缩数据管理程序 GZIP压缩格式是在Sun Solaris操作系统中广泛采用的压缩数据格式 由于在数据压缩过程中可以采用多种类型的压缩算法 因此 压缩文件的压缩比很高 另外 在创建的压缩文件中 定义了用于表述时间和文件属主的时戳(Time Stamp) 可以使文件方便地在网络中传输和交换 GZIP压缩数据文件由一系列的数字构成 而各数字对应如下描述压缩文件信息的字段 ID 缺省值 用于标识GZIP压缩格式 ID 缺省值 用于标识GZIP压缩格式 CM 采用的压缩方法 其值为 ~ 是保留值 标识采用 deflate 压缩方法 FLG 用于标识各占用位的标志 MTIME 记录了最近修改时间 XFL 用于标识采用压缩算法的选项 OS 定义了操作系统类型 XLEN 定义了附加信息段的长度 M 压缩文件说明信息 CRC 记录了CRC 算法采用的循环冗余校验值 上述信息完整描述了GZIP压缩格式数据 当然 基于JDK开发的压缩数据管理程序 不需要明确知道上述压缩数据定义格式 只需要创建相应的管理对象并调用这些对象中定义的方法即可 JDK API中ZIP压缩格式支持对象GZIP压缩格式是在JDK API中定义支持的另外一种数据压缩格式 由上面介绍的GZIP格式数据压缩方法可知 GZIP压缩格式具有更大的压缩比 因此 在Unix操作系统中 这种类型的数据压缩形式的应用十分普及 与JDK API对ZIP压缩格式的支持不同 在JDK API中 只定义了GZIPInputStream和GZIPOutputStream两种类型的流(Stream)对象 用于在基于流的数据传输过程中实现数据压缩 这两个对象的继承定义结构如下所示 java lang Object|+ java io InputStream|+ java io FilterInputStream|+ java util zip InflaterInputStream|+ java util zip GZIPInputStream(java util zip GZIPOutputStream)以采用GZIP格式进行数据输入处理GZIPInputStream对象为例 由上述对象的继承定义结构可以看出 该对象继承了InflaterInputStream流对象 需要说明的是 在ZIP压缩包中 定义了Inflater和Deflater两个对象 用于基于ZLIB压缩库实现多种格式的数据压缩和解压缩 因此 InflaterInputStream流对象的作用是采用ZLIB库作为数据压缩管理的引擎 而GZIPInputStream对象则进一步将流的数据加工进行细化 用于读取GZIP格式的压缩数据 同理 GZIPOutputStream对象用于创建GZIP格式的压缩数据文件 下面 将对两个对象的定义内容进行说明 ●GZIPInputStream对象定义结构 java util zip GZIPInputStream静态成员变量 protected CRC crc 用于说明采用的数据压缩算法为CRC protected boolean eos 说明输入流对象结束读取输入数据 构造方法 GZIPInputStream(InputStream in) 采用默认的缓冲区字节数创建输入流对象 GZIPInputStream(InputStream in int size) 创建由整数类型变量size指定缓冲区字节数的输入流对象 成员方法 该对象只定义了如下两个成员方法 void close() 关闭输入流对象 int read(byte[] buf int off int len) 读取输入流的数据到buf字节数组中 ●GZIPOutputStream对象定义结构 java util zip GZIPOutputStream静态成员变量 protected CRC crc 用于说明采用的数据压缩算法为CRC 构造方法 GZIPOutputStream(OutputStream out) 采用默认的缓冲区字节数创建输出流对象 GZIPOutputStream(OutputStream out int size) 创建由整数类型变量size指定缓冲区字节数的输出流对象 成员方法 void close() 关闭输出流对象 void finish() 结束数据输出 但不关闭输出流对象 void write(byte[] buf int off int len) 将字节数组buf中的内容压缩输出到输出流对象中 创建GZIP压缩格式文件实例经过前面对JDK API中创建GZIP压缩格式文件的相关对象的结构 成员方法定义形式的说明 读者一定会问如何应用这些对象和对象中定义的成员方法呢?请读者看下面的实例代码 //ZipDemo javaimport java io *; import java util zip *; public class GZIPDemo { public static void main(String[] args) { if (args length != ) { System out println("Usage:java GZIPDemo SourceFile DestnFile" + args length); System exit( ); } try { int number; //打开需压缩文件作为文件输入流 FileInputStream fin = new FileInputStream(args[ ]); //建立压缩文件输出流FileOutputStream fout=new FileOutputStream(args[ ]); //建立GZIP压缩输出流 GZIPOutputStream gzout=new GZIPOutputStream(fout); //设定读入缓冲区尺寸byte[] buf=new byte[ ]; while ((number = fin read(buf)) != ) gzout write(buf number); gzout close(); fout close(); fin close(); }catch(IOException e) { System out println(e); } } }上面的程序用于将命令行中指定的文件SourceFile进行压缩 创建GZIP格式的压缩文件DestnFile 在该程序的实现代码中 首先创建用于进行文件输入和输出的FileInputStream和FileOutputStream对象 并以FileOutputStream对象实例为参数创建GZIPOutputStream对象实例 从而为创建GZIP格式压缩文件建立数据流基础 在随后的代码中 利用FileInputStream对象中定义的read方法 从源文件中读取待压缩文件的内容 同时利用GZIPOutputStream对象中定义的write方法将压缩后的数据写出到输出文件中 从而实现数据文件的GZIP格式压缩处理 在Java中创建GZIP格式压缩文件的方法很简单 并且利用WinZip WinRAR等类型的压缩管理软件均能够打开创建的GZIP格式的压缩文件 那么 如何利用JDK API中定义的对象将被压缩的文件解压缩呢?请读者看下一节的内容 GZIP格式文件解压缩实例下面的程序用于将利用JDK API中定义对象的成员方法 将GZIP格式压缩文件进行解压缩 从而恢复压缩原始文件 //UnGZIPDemo javaimport java io *; import java util zip *; public class UnGZIPDemo { public static void main(String[] args) { if (args length != ) { System out println("Usage:java UnGZIPDemo GZIPFile DestnFile"); System exit( ); } try { int number;//建立GZIP压缩文件输入流 FileInputStream fin=new FileInputStream(args[ ]); //建立GZIP解压工作流 GZIPInputStream gzin=new GZIPInputStream(fin); //建立解压文件输出流 FileOutputStream fout=new FileOutputStream(args[ ]); //设定读入缓冲区尺寸byte[] buf=new byte[ ]; while ((nnumber=gzin read(buf buf length)) != ) fout write(buf nnumber); gzin close(); fout close(); fin close(); }catch(IOException e) { System out println(e); } } }在GZIP格式压缩文件解压缩程序代码中 仍然首先创建FileInputStream和FileOutputStream对象 并基于创建的FileInputStream对象创建GZIPInputStream对象 在随后的代码中 调用GZIPInputStream对象中定义的read方法 在从压缩文件中读取数据内容并进行解压缩处理后 将解除压缩后的数据内容利用文件输出流对象进行输出 从而实现数据文件的解压缩处理 小 lishixinzhi/Article/program/Java/hx/201311/27034
怎样用java快速实现zip文件的压缩解压缩
package zip;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Enumeration;
import java.util.zip.CRC32;
import java.util.zip.CheckedOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipOutputStream;
import org.apache.commons.lang3.StringUtils;
public class ZipUtil {
/**
* 递归压缩文件夹
* @param srcRootDir 压缩文件夹根目录的子路径
* @param file 当前递归压缩的文件或目录对象
* @param zos 压缩文件存储对象
* @throws Exception
*/
private static void zip(String srcRootDir, File file, ZipOutputStream zos) throws Exception
{
if (file == null)
{
return;
}
//如果是文件,则直接压缩该文件
if (file.isFile())
{
int count, bufferLen = 1024;
byte data[] = new byte[bufferLen];
//获取文件相对于压缩文件夹根目录的子路径
String subPath = file.getAbsolutePath();
int index = subPath.indexOf(srcRootDir);
if (index != -1)
{
subPath = subPath.substring(srcRootDir.length() + File.separator.length());
}
ZipEntry entry = new ZipEntry(subPath);
zos.putNextEntry(entry);
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
while ((count = bis.read(data, 0, bufferLen)) != -1)
{
zos.write(data, 0, count);
}
bis.close();
zos.closeEntry();
}
//如果是目录,则压缩整个目录
else
{
//压缩目录中的文件或子目录
File[] childFileList = file.listFiles();
for (int n=0; nchildFileList.length; n++)
{
childFileList[n].getAbsolutePath().indexOf(file.getAbsolutePath());
zip(srcRootDir, childFileList[n], zos);
}
}
}
/**
* 对文件或文件目录进行压缩
* @param srcPath 要压缩的源文件路径。如果压缩一个文件,则为该文件的全路径;如果压缩一个目录,则为该目录的顶层目录路径
* @param zipPath 压缩文件保存的路径。注意:zipPath不能是srcPath路径下的子文件夹
* @param zipFileName 压缩文件名
* @throws Exception
*/
public static void zip(String srcPath, String zipPath, String zipFileName) throws Exception
{
if (StringUtils.isEmpty(srcPath) || StringUtils.isEmpty(zipPath) || StringUtils.isEmpty(zipFileName))
{
throw new ParameterException(ICommonResultCode.PARAMETER_IS_NULL);
}
CheckedOutputStream cos = null;
ZipOutputStream zos = null;
try
{
File srcFile = new File(srcPath);
//判断压缩文件保存的路径是否为源文件路径的子文件夹,如果是,则抛出异常(防止无限递归压缩的发生)
if (srcFile.isDirectory() zipPath.indexOf(srcPath)!=-1)
{
throw new ParameterException(ICommonResultCode.INVALID_PARAMETER, "zipPath must not be the child directory of srcPath.");
}
//判断压缩文件保存的路径是否存在,如果不存在,则创建目录
File zipDir = new File(zipPath);
if (!zipDir.exists() || !zipDir.isDirectory())
{
zipDir.mkdirs();
}
//创建压缩文件保存的文件对象
String zipFilePath = zipPath + File.separator + zipFileName;
File zipFile = new File(zipFilePath);
if (zipFile.exists())
{
//检测文件是否允许删除,如果不允许删除,将会抛出SecurityException
SecurityManager securityManager = new SecurityManager();
securityManager.checkDelete(zipFilePath);
//删除已存在的目标文件
zipFile.delete();
}
cos = new CheckedOutputStream(new FileOutputStream(zipFile), new CRC32());
zos = new ZipOutputStream(cos);
//如果只是压缩一个文件,则需要截取该文件的父目录
String srcRootDir = srcPath;
if (srcFile.isFile())
{
int index = srcPath.lastIndexOf(File.separator);
if (index != -1)
{
srcRootDir = srcPath.substring(0, index);
}
}
//调用递归压缩方法进行目录或文件压缩
zip(srcRootDir, srcFile, zos);
zos.flush();
}
catch (Exception e)
{
throw e;
}
finally
{
try
{
if (zos != null)
{
zos.close();
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
/**
* 解压缩zip包
* @param zipFilePath zip文件的全路径
* @param unzipFilePath 解压后的文件保存的路径
* @param includeZipFileName 解压后的文件保存的路径是否包含压缩文件的文件名。true-包含;false-不包含
*/
@SuppressWarnings("unchecked")
public static void unzip(String zipFilePath, String unzipFilePath, boolean includeZipFileName) throws Exception
{
if (StringUtils.isEmpty(zipFilePath) || StringUtils.isEmpty(unzipFilePath))
{
throw new ParameterException(ICommonResultCode.PARAMETER_IS_NULL);
}
File zipFile = new File(zipFilePath);
//如果解压后的文件保存路径包含压缩文件的文件名,则追加该文件名到解压路径
if (includeZipFileName)
{
String fileName = zipFile.getName();
if (StringUtils.isNotEmpty(fileName))
{
fileName = fileName.substring(0, fileName.lastIndexOf("."));
}
unzipFilePath = unzipFilePath + File.separator + fileName;
}
//创建解压缩文件保存的路径
File unzipFileDir = new File(unzipFilePath);
if (!unzipFileDir.exists() || !unzipFileDir.isDirectory())
{
unzipFileDir.mkdirs();
}
//开始解压
ZipEntry entry = null;
String entryFilePath = null, entryDirPath = null;
File entryFile = null, entryDir = null;
int index = 0, count = 0, bufferSize = 1024;
byte[] buffer = new byte[bufferSize];
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
ZipFile zip = new ZipFile(zipFile);
EnumerationZipEntry entries = (EnumerationZipEntry)zip.entries();
//循环对压缩包里的每一个文件进行解压
while(entries.hasMoreElements())
{
entry = entries.nextElement();
//构建压缩包中一个文件解压后保存的文件全路径
entryFilePath = unzipFilePath + File.separator + entry.getName();
//构建解压后保存的文件夹路径
index = entryFilePath.lastIndexOf(File.separator);
if (index != -1)
{
entryDirPath = entryFilePath.substring(0, index);
}
else
{
entryDirPath = "";
}
entryDir = new File(entryDirPath);
//如果文件夹路径不存在,则创建文件夹
if (!entryDir.exists() || !entryDir.isDirectory())
{
entryDir.mkdirs();
}
//创建解压文件
entryFile = new File(entryFilePath);
if (entryFile.exists())
{
//检测文件是否允许删除,如果不允许删除,将会抛出SecurityException
SecurityManager securityManager = new SecurityManager();
securityManager.checkDelete(entryFilePath);
//删除已存在的目标文件
entryFile.delete();
}
//写入文件
bos = new BufferedOutputStream(new FileOutputStream(entryFile));
bis = new BufferedInputStream(zip.getInputStream(entry));
while ((count = bis.read(buffer, 0, bufferSize)) != -1)
{
bos.write(buffer, 0, count);
}
bos.flush();
bos.close();
}
}
public static void main(String[] args)
{
String zipPath = "d:\\ziptest\\zipPath";
String dir = "d:\\ziptest\\rawfiles";
String zipFileName = "test.zip";
try
{
zip(dir, zipPath, zipFileName);
}
catch (Exception e)
{
e.printStackTrace();
}
String zipFilePath = "D:\\ziptest\\zipPath\\test.zip";
String unzipFilePath = "D:\\ziptest\\zipPath";
try
{
unzip(zipFilePath, unzipFilePath, true);
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
java 什么算法压缩文件最小
有三种方式实现java压缩:
1、jdk自带的包java.util.zip.ZipOutputStream,不足之处,文件(夹)名称带中文时,出现乱码问题,实现代码如下:
/**
* 功能:把 sourceDir 目录下的所有文件进行 zip 格式的压缩,保存为指定 zip 文件
* @param sourceDir 如果是目录,eg:D:\\MyEclipse\\first\\testFile,则压缩目录下所有文件;
* 如果是文件,eg:D:\\MyEclipse\\first\\testFile\\aa.zip,则只压缩本文件
* @param zipFile 最后压缩的文件路径和名称,eg:D:\\MyEclipse\\first\\testFile\\aa.zip
*/
public File doZip(String sourceDir, String zipFilePath) throws IOException {
File file = new File(sourceDir);
File zipFile = new File(zipFilePath);
ZipOutputStream zos = null;
try {
// 创建写出流操作
OutputStream os = new FileOutputStream(zipFile);
BufferedOutputStream bos = new BufferedOutputStream(os);
zos = new ZipOutputStream(bos);
String basePath = null;
// 获取目录
if(file.isDirectory()) {
basePath = file.getPath();
}else {
basePath = file.getParent();
}
zipFile(file, basePath, zos);
}finally {
if(zos != null) {
zos.closeEntry();
zos.close();
}
}
return zipFile;
}
/**
* @param source 源文件
* @param basePath
* @param zos
*/
private void zipFile(File source, String basePath, ZipOutputStream zos)
throws IOException {
File[] files = null;
if (source.isDirectory()) {
files = source.listFiles();
} else {
files = new File[1];
files[0] = source;
}
InputStream is = null;
String pathName;
byte[] buf = new byte[1024];
int length = 0;
try{
for(File file : files) {
if(file.isDirectory()) {
pathName = file.getPath().substring(basePath.length() + 1) + "/";
zos.putNextEntry(new ZipEntry(pathName));
zipFile(file, basePath, zos);
}else {
pathName = file.getPath().substring(basePath.length() + 1);
is = new FileInputStream(file);
BufferedInputStream bis = new BufferedInputStream(is);
zos.putNextEntry(new ZipEntry(pathName));
while ((length = bis.read(buf)) 0) {
zos.write(buf, 0, length);
}
}
}
}finally {
if(is != null) {
is.close();
}
}
}
2、使用org.apache.tools.zip.ZipOutputStream,代码如下,
package net.szh.zip;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.zip.CRC32;
import java.util.zip.CheckedOutputStream;
import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipOutputStream;
public class ZipCompressor {
static final int BUFFER = 8192;
private File zipFile;
public ZipCompressor(String pathName) {
zipFile = new File(pathName);
}
public void compress(String srcPathName) {
File file = new File(srcPathName);
if (!file.exists())
throw new RuntimeException(srcPathName + "不存在!");
try {
FileOutputStream fileOutputStream = new FileOutputStream(zipFile);
CheckedOutputStream cos = new CheckedOutputStream(fileOutputStream,
new CRC32());
ZipOutputStream out = new ZipOutputStream(cos);
String basedir = "";
compress(file, out, basedir);
out.close();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
private void compress(File file, ZipOutputStream out, String basedir) {
/* 判断是目录还是文件 */
if (file.isDirectory()) {
System.out.println("压缩:" + basedir + file.getName());
this.compressDirectory(file, out, basedir);
} else {
System.out.println("压缩:" + basedir + file.getName());
this.compressFile(file, out, basedir);
}
}
/** 压缩一个目录 */
private void compressDirectory(File dir, ZipOutputStream out, String basedir) {
if (!dir.exists())
return;
File[] files = dir.listFiles();
for (int i = 0; i files.length; i++) {
/* 递归 */
compress(files[i], out, basedir + dir.getName() + "/");
}
}
/** 压缩一个文件 */
private void compressFile(File file, ZipOutputStream out, String basedir) {
if (!file.exists()) {
return;
}
try {
BufferedInputStream bis = new BufferedInputStream(
new FileInputStream(file));
ZipEntry entry = new ZipEntry(basedir + file.getName());
out.putNextEntry(entry);
int count;
byte data[] = new byte[BUFFER];
while ((count = bis.read(data, 0, BUFFER)) != -1) {
out.write(data, 0, count);
}
bis.close();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
3、可以用ant中的org.apache.tools.ant.taskdefs.Zip来实现,更加简单。
package net.szh.zip;
import java.io.File;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.taskdefs.Zip;
import org.apache.tools.ant.types.FileSet;
public class ZipCompressorByAnt {
private File zipFile;
public ZipCompressorByAnt(String pathName) {
zipFile = new File(pathName);
}
public void compress(String srcPathName) {
File srcdir = new File(srcPathName);
if (!srcdir.exists())
throw new RuntimeException(srcPathName + "不存在!");
Project prj = new Project();
Zip zip = new Zip();
zip.setProject(prj);
zip.setDestFile(zipFile);
FileSet fileSet = new FileSet();
fileSet.setProject(prj);
fileSet.setDir(srcdir);
//fileSet.setIncludes("**/*.java"); 包括哪些文件或文件夹 eg:zip.setIncludes("*.java");
//fileSet.setExcludes(...); 排除哪些文件或文件夹
zip.addFileset(fileSet);
zip.execute();
}
}
测试一下
package net.szh.zip;
public class TestZip {
public static void main(String[] args) {
ZipCompressor zc = new ZipCompressor("E:\\szhzip.zip");
zc.compress("E:\\test");
ZipCompressorByAnt zca = new ZipCompressorByAnt("E:\\szhzipant.zip");
zca.compress("E:\\test");
}
}
java压缩文件最高压缩率
更好的压缩率(使用gzip时具有最佳压缩级别 LZMA达到38%,bzip2 34% 25%)。 压缩率的增加主要体现在二进制文件上。 解压缩时间比bzip2快(3-...
java 怎么把文件压缩
用Eclipse可以将java文件打成.jar包,再用exe4打成exe文件,不过exe4貌似不是免费的,打成jar包的具体过程是File-Export-java-jar file-next-选择工程中的java文件-下面选择jar包的路径-next-main class礼选择主类-finish,就ok 了,注意,只有你的机子上安装了jre/jdk之后打出来的才是jar包,直接点击才能运行,不然打出来的是压缩包,也就是说jar包只有在装了java运行环境的机子上才可以运行
java精度压缩的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于Java压缩、java精度压缩的信息别忘了在本站进行查找喔。