资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

关于java怎么压缩代码 java代码压缩文件

如何使用JAVA代码压缩PDF文件

用java代码压缩应用到程序了,代码一般是比较复杂的,对pdf文件的mate标签优化,这类标签包括三类,pdf文件不是网页就是个文件,何况我们可以用pdf压缩工具压缩,下面有个解决方法,楼主可以做参照。

成都创新互联专注为客户提供全方位的互联网综合服务,包含不限于成都网站制作、成都网站设计、延庆网络推广、微信小程序开发、延庆网络营销、延庆企业策划、延庆品牌公关、搜索引擎seo、人物专访、企业宣传片、企业代运营等,从售前售中售后,我们都将竭诚为您服务,您的肯定,是我们最大的嘉奖;成都创新互联为所有大学生创业者提供延庆建站搭建服务,24小时服务热线:13518219792,官方网址:www.cdcxhl.com

1:点击打开工具,打开主页面上有三个功能进行选择,我们选择pdf文件压缩。

2:这这个页面中我们选择pdf文件在这里打开,点击“添加文件”按钮将文件添加进来。

3:然后在页面中点击“开始压缩”就可以开始压缩文件了。

4:压缩完成的文件页面会显示已经完成。

搭建Java环境如何解压缩

具体解压缩方法如下:

Java压缩解压缩文件的方法有,第一中借助javajdk自带的ZipOutputStream和ZipInputStream。第二种,借助第三方jar,例如ApacheCommonsCompress和Ant。

前提,需要将Ant的ant、jar和ant-launcher、jar添加到classpath中。先创建一个Expander类,该类继承了Ant的org、apache、tools、ant、taskdefs、Expand类。

第二步:使用Expander类。

用java如何实现压缩字符串?

package javase1.day02;\x0d\x0a /**\x0d\x0a * 1)一种字符串压缩算法\x0d\x0a * str ="aaaabbccccddeaaa"\x0d\x0a * 压缩为:"4a2b4c2d1e3a"\x0d\x0a * 原理实现:\x0d\x0a * str = "aaaabbccccddeaaa"\x0d\x0a * \x0d\x0a * c = str.charAt(i)//c是每个字符\x0d\x0a * 1) 初始化\x0d\x0a * StringBuilder buf = new StringBuilder();\x0d\x0a * int count = 0;代表相同的字符个数\x0d\x0a * char ch = str.charAt(0);代表正在统计的相同字符'a' \x0d\x0a * 2) 从i=1开始迭代每个字符\x0d\x0a * c = str.charAt(i);//c是每个当前字符\x0d\x0a * 3) 检查当前字符c与被统计ch是否一致\x0d\x0a * 如果一致 count++\x0d\x0a * 否则(不一致)\x0d\x0a * 向缓冲区buf增加count+ch\x0d\x0a * count=0,ch=c;\x0d\x0a * 3)没有下个字符就结束\x0d\x0a * 4)还有字符串吗?回到2)\x0d\x0a * \x0d\x0a * 2)实现还原算法\x0d\x0a * str = "4a2b4c2d1e3a";\x0d\x0a * i\x0d\x0a */\x0d\x0apublic class Demo5 {\x0d\x0a public static void main(String[] args) {\x0d\x0a String s = comp("aaaawwwwe");\x0d\x0a System.out.println(s);\x0d\x0a// System.out.println(decomp(s));\x0d\x0a \x0d\x0a }\x0d\x0a public static String comp(String str){\x0d\x0a int i = 1;\x0d\x0a StringBuilder buf = new StringBuilder();\x0d\x0a int count = 1;\x0d\x0a char ch = str.charAt(0);\x0d\x0a for(;;){\x0d\x0a char c = i==str.length() ? '\10':str.charAt(i);\x0d\x0a if(c==ch){\x0d\x0a count++;\x0d\x0a }else{\x0d\x0a if(count == 1)\x0d\x0a buf.append(ch);\x0d\x0a else \x0d\x0a buf.append(count).append(ch);\x0d\x0a count=1;\x0d\x0a ch = c;\x0d\x0a } \x0d\x0a i++;\x0d\x0a if(i==str.length()+1){\x0d\x0a break;\x0d\x0a } \x0d\x0a }\x0d\x0a return buf.toString();\x0d\x0a \x0d\x0a }\x0d\x0a}

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怎么压缩代码 java代码压缩文件
网页地址:http://cdkjz.cn/article/dddoogs.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220