压缩csv文件会抛出一个;至少有一个ZipEntry“;使用Java
我试图做的是压缩生成的csv文件。在这里的代码出现之前,该文件不会产生任何问题。因此,代码在压缩csv文件会抛出一个;至少有一个ZipEntry“;使用Java,java,zip,Java,Zip,我试图做的是压缩生成的csv文件。在这里的代码出现之前,该文件不会产生任何问题。因此,代码在zos.close()处抛出异常 try { FileOutputStream fos = new FileOutputStream(file.getPath()); ZipOutputStream zos = new ZipOutputStream(fos); FileInputStream in = new FileInputStream(file.getPath());
zos.close()处抛出异常
try {
FileOutputStream fos = new FileOutputStream(file.getPath());
ZipOutputStream zos = new ZipOutputStream(fos);
FileInputStream in = new FileInputStream(file.getPath());
String fullCSVFileName = file.getName();
String fullFileName = fullCSVFileName.substring(0, fullCSVFileName.length()-3);
String fullZipFileName = fullFileName + "zip";
ZipEntry ze= new ZipEntry(fullZipFileName);
if(ze != null) zos.putNextEntry(ze);
fos = new FileOutputStream("C:\\sourceLocation\\"+fullZipFileName);
zos = new ZipOutputStream(fos);
byte[] buffer = new byte[1024];
int len;// = in.read(buffer);
while ((len = in.read(buffer)) > 0) {
Logger.debug("in Loop, len = " + len);
zos.write(buffer, 0, len);
}
in.close();
zos.closeEntry();
zos.close();
Logger.debug("Zipping complete!");
} catch(IOException ex) {
Logger.error(ex);
}
已更正的代码
try{
String fullCSVFileName = file.getName();
String fullFileName = fullCSVFileName.substring(0, fullCSVFileName.length()-3);
String fullZipFileName = fullFileName + "zip";
FileOutputStream fos = new FileOutputStream("C:\\sourceLocation\\"+fullZipFileName);
ZipOutputStream zos = new ZipOutputStream(fos);
FileInputStream in = new FileInputStream(file.getPath());
ZipEntry ze= new ZipEntry(fullZipFileName);
if(ze != null){
zos.putNextEntry(ze);
}
byte[] buffer = new byte[1024];
int len;
while ((len = in.read(buffer)) > 0) {
zos.write(buffer, 0, len);
}
in.close();
zos.closeEntry();
zos.close();
Logger.debug("Zipping complete!");
}catch(IOException ex){
Logger.error(ex);
}
您可以在代码顶部创建一次fos
和zos
:
FileOutputStream fos = new FileOutputStream(file.getPath());
ZipOutputStream zos = new ZipOutputStream(fos);
然后添加一个ZipEntry:
if(ze != null) zos.putNextEntry(ze);
然后,稍后再重新定义它们:
fos = new FileOutputStream("C:\\sourceLocation\\"+fullZipFileName);
zos = new ZipOutputStream(fos);
然后关闭新的zos。你从未关闭过,也没有写信给第一个zos(有一个ZipEntry),也没有在第二个ZipEntry中添加ZipEntry(你试图关闭而没有任何ZipEntry)。因此,至少有一个ZipEntry错误
-----------编辑-----------------
尝试添加zos.finish()
,另外,您的close()
方法应该位于finally块中
ZipOutputStream zos = null;
FileInputStream in = null;
try{
String fullCSVFileName = file.getName();
String fullFileName = fullCSVFileName.substring(0, fullCSVFileName.length()-3);
String fullZipFileName = fullFileName + "zip";
ZipOutputStream zos = new ZipOutputStream(
new FileOutputStream("C:\\sourceLocation\\"+fullZipFileName));
in = new FileInputStream(file.getPath());
zos.putNextEntry( new ZipEntry(fullZipFileName) );
byte[] buffer = new byte[1024];
int len;
while ((len = in.read(buffer)) > 0) {
zos.write(buffer, 0, len);
}
zos.finish();
Logger.debug("Zipping complete!");
}catch(IOException ex){
Logger.error(ex);
}finally {
if ( zos != null ) {
try {
zos.close();
} catch ( Exception e ) {}
}
if ( in != null ) {
try {
in.close();
} catch ( Exception e ) {}
}
}
你可以把你的堆栈跟踪。使用更正的代码
我得到一个zip文件,它位于另一个zip文件中,名称相同,已损坏或格式未知
我在编辑中进行了更改,我能够创建zip文件。但是,当我打开zipfile时,我看到其中有另一个同名的zipfile,打开它会给我一个存档文件的格式未知或已损坏