java ZipOutputStream 问题
我正在尝试编写一个 java ZIP util 类,如下所示:
package fdbank.util;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
/**
* 解压缩工具类
* @author ggfan@amarsoft
*
*/
public class ZIPUtil {
private static void zip(File[] files, String dest) throws IOException{
ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(new File("dest")));
for(File file : files){
zip(file, zos);
}
zos.close();
}
private static void zip(File file, ZipOutputStream zos) throws IOException{
byte[] buf = new byte[2048];
@SuppressWarnings("unused")
int bytes = 0;
if(file.isDirectory()){
ZipEntry entry = new ZipEntry(file.getName());
zos.putNextEntry(entry);
for(File subFile : file.listFiles()){
zip(subFile, zos);
}
zos.closeEntry();
}
FileInputStream fis = new FileInputStream(file);
System.out.println(file.getName());
ZipEntry entry = new ZipEntry(file.getName());
zos.putNextEntry(entry);
while((bytes = fis.read(buf)) != -1){
zos.write(buf);
}
zos.closeEntry();
fis.close();
}
public static void compress(int archiveType, File[] files, String dest){
}
public static void main(String[] args){
try {
System.out.println("gan !!!!");
zip(new File[]{new File("F:\\ziptest\\1.bmp")},"c:\\ziptest.zip");
} catch (IOException e) {
e.printStackTrace();
}
}
}
我运行它,没有错误,但根本没有创建 zip 文件! 我的代码有什么问题吗?
I am trying to write a java ZIP util class as below:
package fdbank.util;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
/**
* 解压缩工具类
* @author ggfan@amarsoft
*
*/
public class ZIPUtil {
private static void zip(File[] files, String dest) throws IOException{
ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(new File("dest")));
for(File file : files){
zip(file, zos);
}
zos.close();
}
private static void zip(File file, ZipOutputStream zos) throws IOException{
byte[] buf = new byte[2048];
@SuppressWarnings("unused")
int bytes = 0;
if(file.isDirectory()){
ZipEntry entry = new ZipEntry(file.getName());
zos.putNextEntry(entry);
for(File subFile : file.listFiles()){
zip(subFile, zos);
}
zos.closeEntry();
}
FileInputStream fis = new FileInputStream(file);
System.out.println(file.getName());
ZipEntry entry = new ZipEntry(file.getName());
zos.putNextEntry(entry);
while((bytes = fis.read(buf)) != -1){
zos.write(buf);
}
zos.closeEntry();
fis.close();
}
public static void compress(int archiveType, File[] files, String dest){
}
public static void main(String[] args){
try {
System.out.println("gan !!!!");
zip(new File[]{new File("F:\\ziptest\\1.bmp")},"c:\\ziptest.zip");
} catch (IOException e) {
e.printStackTrace();
}
}
}
I run it ,no error but the zip file not created at all!!!
what's wrong with my code ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您总是写入名为“dest”的文件,并忽略名为
dest
的String
参数(其值为c:\ziptest.zip
)。将第一个
zip()
方法第一行的"dest"
替换为dest
。另外:您不能忽略
fis.read()
的返回值:如果read()
没有填充缓冲区buf
,那么您必须将其告诉相应的 write() 调用:You're always writing to a file called "dest" and ignore the
String
parameter calleddest
(with the valuec:\ziptest.zip
).Replace
"dest"
withdest
on the first line of your firstzip()
method.Also: you must not ignore the return value of
fis.read()
: Ifread()
doesn't fill the bufferbuf
, then you must tell that to the correspondingwrite()
call: