RELATEED CONSULTING
相关咨询
选择下列产品马上在线沟通
服务时间:8:30-17:00
你可能遇到了下面的问题
关闭右侧工具栏

新闻中心

这里有您想知道的互联网营销解决方案
java上传压缩包代码 java上传zip压缩文件

java 新手 这段压缩文件的简答代码哪里有问题?

这个程序是没有任何问题的,就是你创建文件路径的时候估计没创建好,从你的程序中可以看到,首先创建一个输出流ZipOutputStream 对象,然后创建一个输入流对象FileInputStream,通过输入流读取文件testfile.txt的内容,然后把这个内容通过输出流输出到压缩文件testfile.zip。

成都创新互联专注于企业全网营销推广、网站重做改版、合浦网站定制设计、自适应品牌网站建设、成都h5网站建设购物商城网站建设、集团公司官网建设、成都外贸网站建设、高端网站制作、响应式网页设计等建站业务,价格优惠性价比高,为合浦等各大城市提供网站开发制作服务。

根据你的程序,首先要在E盘下创建这样一个路径,E:\java\java works\testfile,然后在文件夹testfile下面创建文件testfile.txt,这个时候你可以在testfile.txt文件中写上一些内容,因为你是从这个文件读取内容的,如果你事先不创建这个文件的话,那么就会抛出找不到文件的异常。

我新建的文件路径如下图所示:

运行程序之后,多了一个压缩文件,解压testfile.zip文件,里面有testfile.txt文件培薯册,并且内容是我刚刚输入的内容,压缩成功:

你的程序我做了一点小的改动,代码如下:

package life;

import java.io.*;

//import java.util.*;//这个包不需要。

import java.util.zip.*;

public class practice {

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

File myfile = new File("e:/java/java works/testfile/testfile.txt");//创建一个File对象。

File newfile = new File("e:/java/java works/testfile/testfile.zip");//创建一个File对象。

ZipOutputStream zip = new ZipOutputStream(new FileOutputStream(newfile));// 以输出流来创建ZipOutputStream对象。

String e = "testfile.txt";//这里我改了一下,指定压缩文件之后压缩包所包含的文件的名称和文件格式,如果是配宏写String e=""的话,压缩成功之后,压缩包里面的文件是一个不可识别的文件,文件名为testfile但是没有后缀名.txt。

zip.putNextEntry(new ZipEntry(e));

System.out.println(e);//打印压缩包里面文件的名称。

FileInputStream 手孝in = new FileInputStream(myfile);//以字节流从文件testfile.txt读取内容。

int i = 0;

while ((i = in.read()) != -1) {

zip.write(i);//把读取到的内容写到压缩文件,压缩文件名称为testfile.zip。

}

zip.close();

in.close();

}

}

通过这个程序我又学到了新的东西,受益匪浅。

java如何解压页面上传到服务器的zip文件

直接通过工具类进行解压或者压缩文件即可。

import java.io.BufferedInputStream;

import java.io.BufferedOutputStream;

import java.io.Closeable;

import java.io.File;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.InputStream;

import java.util.Enumeration;

import java.util.zip.ZipEntry;

import java.util.zip.ZipFile;

/**

*

* @author gdb

*/

public class ZipUtilAll {

public static final int DEFAULT_BUFSIZE = 1024 * 16;

/**

* 解压Zip文件简态

*

* @param srcZipFile

* @param destDir

* @throws IOException

*/

public static void unZip(File srcZipFile, String destDir) throws IOException

{

ZipFile zipFile = new ZipFile(srcZipFile);

unZip(zipFile, destDir);

}

/**

* 解压Zip文件

*

* @param srcZipFile

* @param destDir

* @throws IOException

*/

public static void unZip(String srcZipFile, String destDir) throws IOException

{

ZipFile zipFile = new ZipFile(srcZipFile);

unZip(zipFile, destDir);

}

/**

* 解压Zip文件

*

* @param zipFile

* @param destDir

* @throws IOException

*/

public static void unZip(ZipFile zipFile, String destDir) throws IOException

{

Enumeration? extends ZipEntry entryEnum = zipFile.entries();

ZipEntry entry = null;

while (entryEnum.hasMoreElements()) {

entry = entryEnum.nextElement();

File destFile = new File(destDir + entry.getName());

if (entry.isDirectory()) {

destFile.mkdirs();

}

else {

destFile.getParentFile().mkdirs();

InputStream eis = zipFile.getInputStream(entry);

System.out.println(eis.read());

write(eis, destFile);

}

}

}

/**

* 将输入流中的数清咐大据写到指定文答竖件

*

* @param inputStream

* @param destFile

*/

public static void write(InputStream inputStream, File destFile) throws IOException

{

BufferedInputStream bufIs = null;

BufferedOutputStream bufOs = null;

try {

bufIs = new BufferedInputStream(inputStream);

bufOs = new BufferedOutputStream(new FileOutputStream(destFile));

byte[] buf = new byte[DEFAULT_BUFSIZE];

int len = 0;

while ((len = bufIs.read(buf, 0, buf.length)) 0) {

bufOs.write(buf, 0, len);

}

} catch (IOException ex) {

throw ex;

} finally {

close(bufOs, bufIs);

}

}

/**

* 安全关闭多个流

*

* @param streams

*/

public static void close(Closeable... streams)

{

try {

for (Closeable s : streams) {

if (s != null)

s.close();

}

} catch (IOException ioe) {

ioe.printStackTrace(System.err);

}

}

/**

* @param args

* @throws java.lang.Exception

*/

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

{

// unZip(new File(ZipDemo.class.getResource("D:/123/HKRT-B2B.zip").toURI()), "D:/123/");

unZip("D:/123/123.zip", "D:/123/");

// new File();

}

}


网页标题:java上传压缩包代码 java上传zip压缩文件
转载来源:http://sczitong.cn/article/dsppsph.html