java代码的压缩包,java下载压缩包代码

java代码实现 导出zip包,无法打开zip压缩包

package com.lch.test;

发展壮大离不开广大客户长期以来的信赖与支持,我们将始终秉承“诚信为本、服务至上”的服务理念,坚持“二合一”的优良服务模式,真诚服务每家企业,认真做好每个细节,不断完善自我,成就企业,实现共赢。行业涉及铜雕雕塑等,在成都网站建设全网营销推广、WAP手机网站、VI设计、软件开发等项目上具有丰富的设计经验。

import java.util.Enumeration;

import java.util.zip.ZipEntry;

import java.util.zip.ZipFile;

public class ZIP {

public static void main(String[] argv) throws Exception {

ZipFile zf = new ZipFile("E:\\wk\\LBSLEMIS201106141057\\LBSLEMIS\\test\\com\\lch\\test\\filename.zip");

for (Enumeration entries = zf.entries(); entries.hasMoreElements();) {

String zipEntryName = ((ZipEntry) entries.nextElement()).getName();

System.out.println(zipEntryName);

}

}

}

用javad 的ZipFile类的ZipEntry方法试一下 找到ZIP里面的ZipEntry方法 读取Zip里面压缩文件的内容

有可能会引用外包

你好,我不知道你说的dzp是什么格式文件,但如果是zip的压缩文件,可以看下我的这段代码

ZipFile file = new ZipFile("d:\\1.zip");

ZipEntry entry = file.getEntry("1.xml"); //假如压缩包里的文件名是1.xml

InputStream in=file.getInputStream(entry);

最后就是按照java中一贯的流的处理方式即可

可以不解压,zip包里的一个对象就是一个ZipEntry

找到你想要的那个ZipEntry,用文流写出来就可以了。追问通过ZipEntry,然后用流就可以读出里面的内容了吗?谢谢指点!

回答/**

* 解压

* @param root 输出目标

* @param zipfile zip文件

*/

protected void unzip(File root, File zipfile, String file) throws Exception {

// 解压文件不存在时返回

if (!zipfile.exists()) {

return;

}

// 释放目录不存时创建

if (!root.exists()) {

root.mkdirs();

}

// 释放目录不为目录时返回

if (!root.isDirectory()) {

return;

}

FileInputStream fin = new FileInputStream(zipfile);

ZipInputStream zin = new ZipInputStream(fin);

ZipEntry entry = null;

while ((entry = zin.getNextEntry()) != null) {

// if (!entry.getName().endsWith(file)) {

// continue;

// }

File tmp = new File(root, entry.getName());

if (entry.isDirectory()) {

tmp.mkdirs();

} else {

byte[] buff = new byte[4096];

int len = 0;

tmp.getParentFile().mkdirs();

FileOutputStream fout = new FileOutputStream(tmp);

while ((len = zin.read(buff)) != -1) {

fout.write(buff, 0, len);

}

zin.closeEntry();

fout.close();

}

}

}

这里完整的解压代码。

// if (!entry.getName().endsWith(file)) {

// continue;

// }

这段打开就是只解出一个你指定的文件。

下面是测试用的。

public static void main(String[] args) throws Exception {

new CommonFiles().unzip(new File("D:\\"), new File("D:\\test.zip"),"file.txt");

}

这个例子会在D盘生成型个test文件夹,file.txt就会在里面,(里面也可能会有多个文件夹,这个取决于压缩包里文件的度)

如何使用java压缩文件夹成为zip包

在JDK中有一个zip工具类:

java.util.zip    Provides classes for reading and writing the standard ZIP and

GZIP file formats.

使用此类可以将文件夹或者多个文件进行打包压缩操作。

在使用之前先了解关键方法:

ZipEntry(String name)         Creates a new zip entry with the specified name.

使用ZipEntry的构造方法可以创建一个zip压缩文件包的实例,然后通过ZipOutputStream将待压缩的文件以流的形式写进该压缩包中。具体实现代码如下:

import java.io.BufferedInputStream;  

import java.io.BufferedOutputStream;  

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;  

/** 

* 将文件夹下面的文件 

* 打包成zip压缩文件 

*  

* @author admin 

*/  

public final class FileToZip {  

private FileToZip(){}  

/** 

* 将存放在sourceFilePath目录下的源文件,打包成fileName名称的zip文件,并存放到zipFilePath路径下 

* @param sourceFilePath :待压缩的文件路径 

* @param zipFilePath :压缩后存放路径 

* @param fileName :压缩后文件的名称 

* @return 

*/  

public static boolean fileToZip(String sourceFilePath,String zipFilePath,String fileName){  

boolean flag = false;  

File sourceFile = new File(sourceFilePath);  

FileInputStream fis = null;  

BufferedInputStream bis = null;  

FileOutputStream fos = null;  

ZipOutputStream zos = null;  

if(sourceFile.exists() == false){  

System.out.println("待压缩的文件目录:"+sourceFilePath+"不存在.");  

}else{  

try {  

File zipFile = new File(zipFilePath + "/" + fileName +".zip");  

if(zipFile.exists()){  

System.out.println(zipFilePath + "目录下存在名字为:" + fileName +".zip" +"打包文件.");  

}else{  

File[] sourceFiles = sourceFile.listFiles();  

if(null == sourceFiles || sourceFiles.length1){  

System.out.println("待压缩的文件目录:" + sourceFilePath + "里面不存在文件,无需压缩.");  

}else{  

fos = new FileOutputStream(zipFile);  

zos = new ZipOutputStream(new BufferedOutputStream(fos));  

byte[] bufs = new byte[1024*10];  

for(int i=0;isourceFiles.length;i++){  

//创建ZIP实体,并添加进压缩包  

ZipEntry zipEntry = new ZipEntry(sourceFiles[i].getName());  

zos.putNextEntry(zipEntry);  

//读取待压缩的文件并写进压缩包里  

fis = new FileInputStream(sourceFiles[i]);  

bis = new BufferedInputStream(fis, 1024*10);  

int read = 0;  

while((read=bis.read(bufs, 0, 1024*10)) != -1){  

zos.write(bufs,0,read);  

}  

}  

flag = true;  

}  

}  

} catch (FileNotFoundException e) {  

e.printStackTrace();  

throw new RuntimeException(e);  

} catch (IOException e) {  

e.printStackTrace();  

throw new RuntimeException(e);  

} finally{  

//关闭流  

try {  

if(null != bis) bis.close();  

if(null != zos) zos.close();  

} catch (IOException e) {  

e.printStackTrace();  

throw new RuntimeException(e);  

}  

}  

}  

return flag;  

}  

public static void main(String[] args){  

String sourceFilePath = "D:\\TestFile";  

String zipFilePath = "D:\\tmp";  

String fileName = "12700153file";  

boolean flag = FileToZip.fileToZip(sourceFilePath, zipFilePath, fileName);  

if(flag){  

System.out.println("文件打包成功!");  

}else{  

System.out.println("文件打包失败!");  

}  

}  

}

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

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

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

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

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

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

如何用java创建一个加密的压缩包

下面的示例代码演示如何创建zip压缩包。

首先需要由需要压缩的文件创建一个InputStream对象,然后读取文件内容写入到ZipOutputStream中。

ZipOutputStream类接受FileOutputStream作为参数。创建号ZipOutputStream对象后需要创建一个zip entry,然后写入。

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.IOException;

import java.util.zip.ZipEntry;

import java.util.zip.ZipOutputStream;

/**

*

* @author outofmemory.cn

*/

public class Main {

/**

* Creates a zip file

*/

public void createZipFile() {

try {

String inputFileName = "test.txt";

String zipFileName = "compressed.zip";

//Create input and output streams

FileInputStream inStream = new FileInputStream(inputFileName);

ZipOutputStream outStream = new ZipOutputStream(new FileOutputStream(zipFileName));

// Add a zip entry to the output stream

outStream.putNextEntry(new ZipEntry(inputFileName));

byte[] buffer = new byte[1024];

int bytesRead;

//Each chunk of data read from the input stream

//is written to the output stream

while ((bytesRead = inStream.read(buffer)) 0) {

outStream.write(buffer, 0, bytesRead);

}

//Close zip entry and file streams

outStream.closeEntry();

outStream.close();

inStream.close();

} catch (IOException ex) {

ex.printStackTrace();

}

}

/**

* @param args the command line arguments

*/

public static void main(String[] args) {

new Main().createZipFile();

}

当前文章:java代码的压缩包,java下载压缩包代码
地址分享:https://www.cdcxhl.com/article22/dsiccjc.html

成都网站建设公司_创新互联,为您提供响应式网站定制开发建站公司外贸建站小程序开发面包屑导航

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联

小程序开发