我直接从oracle网站复制了这段代码。我在d:\ barcode中有两个.png文件。当我运行此代码时,myfigs.zip在d:驱动器中创建,但它是corruped和0kb大小。
代码: -
public class Zip {
static final int BUFFER = 2048;
public static void main (String argv[]) {
try {
BufferedInputStream origin = null;
FileOutputStream dest = new
FileOutputStream("D:\\myfigs.zip");
CheckedOutputStream checksum = new
CheckedOutputStream(dest, new Adler32());
ZipOutputStream out = new
ZipOutputStream(new
BufferedOutputStream(checksum));
//out.setMethod(ZipOutputStream.DEFLATED);
byte data[] = new byte[BUFFER];
// get a list of files from current directory
File f = new File("D:\\barcode");
String files[] = f.list();
for (int i=0; i<files.length; i++) {
System.out.println("Adding: "+files[i]);
FileInputStream fi = new FileInputStream(files[i]);
origin = new BufferedInputStream(fi, BUFFER);
ZipEntry entry = new ZipEntry(files[i]);
out.putNextEntry(entry);
int count;
while((count = origin.read(data, 0,
BUFFER)) != -1) {
out.write(data, 0, count);
}
origin.close();
}
out.close();
System.out.println("checksum: "+checksum.getChecksum().getValue());
} catch(Exception e) {
e.printStackTrace();
}
}
}
在调试模式下运行代码时添加更多信息代码成功编译到FileInputStream fi行然后在那里停止。抛出的错误是
java.io.FileNotFoundException: barcode.png (The system cannot find the file specified)
Adding: barcode.png
at java.io.FileInputStream.open0(Native Method)
at java.io.FileInputStream.open(FileInputStream.java:195)
at java.io.FileInputStream.<init>(FileInputStream.java:138)
at java.io.FileInputStream.<init>(FileInputStream.java:93)
答案 0 :(得分:1)
例外说&#34;文件未找到&#34;。你的&#34;添加&#34; print语句给出了答案:
Adding: barcode.png
文件&#34; barcode.png&#34;与文件&#34; D:\ barcode \ barcode.png&#34;不同。您只是在寻找名为&#34; barcode.png&#34;的文件。无论您当前的工作目录设置为什么,它都不存在。
根据list()
的文档以及您对打印输出和异常的观察得出的结论:
表示目录本身的名称和目录的父目录不包含在结果中。每个字符串都是文件名而不是完整路径。
所以你需要:
File
has to offer并查看是否有一个可以帮助您完全避免此问题。结合其他小调:
答案 1 :(得分:-2)
您可以使用以下代码来压缩一个文件:
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;
/**
* Create by Cuder
*
*/
public class SampleZipFile {
/**
* Create by Cuder
*/
public static void main(String[] args) {
ZipOutputStream zipOutputStream = null;
FileInputStream fileInputStream = null;
try {
File fileInput = new File(
"D:\\eclipse4.4\\workspace\\SampleJava\\resource\\sampleZipFile.txt");
File fileOutput = new File(
"D:\\eclipse4.4\\workspace\\SampleJava\\resource\\sampleZipFile.zip");
FileOutputStream fileOutputStream = new FileOutputStream(fileOutput);
zipOutputStream = new ZipOutputStream(fileOutputStream);
fileInputStream = new FileInputStream(fileInput);
ZipEntry entry = new ZipEntry(fileInput.getName());
zipOutputStream.putNextEntry(entry);
byte[] buf = new byte[1024];
int byteRead = 0;
while ((byteRead = fileInputStream.read(buf)) > 0) {
zipOutputStream.write(buf, 0, byteRead);
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
if (null != fileInputStream) {
fileInputStream.close();
}
if (null != zipOutputStream) {
zipOutputStream.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}