zip file creation problem
I have to add two files in a zip archive.
I get this message error and i can't resolve it
java.util.zip.ZipException: STORED entry missing size, compressed size, or crc-32
here is the part code i use to create the zip file :
FileOutputStream tempo =new FileOutputStream(temp);
JarOutputStream jarIt =new JarOutputStream(tempo);
jarIt.setMethod(JarOutputStream.STORED);
zipentry =new ZipEntry("foo1.class");
zipentry.setMethod(zipentry.STORED);
zipentry.setSize(save1.length);
jarIt.putNextEntry(zipentry);
jarIt.write(save1);
jarIt.flush();
jarIt.flush();
jarIt.finish();
jarIt.closeEntry();
the file is reading in one piece in the byte[] save1 outside of the zipping function.
I'm really stuck and i haven't found any clue to that problem in the forum.
Thanks for any help solving this problem.
Marc
try:
public String generateZip(){
String fileNameZip;
int pos = fileName.lastIndexOf('.');
fileNameZip = fileName.substring(0,pos);
fileNameZip = fileNameZip+".zip";
File f = new File(dirZip, fileNameZip);
if(f.canRead())
{
// Datei ist vorhanden
System.out.println("datei vorhanden");
}else{//datei ist nicht vorhanden
byte[] data = new byte[4096];
Checksum check = new Adler32();
try{
ZipOutputStream out = new ZipOutputStream(new FileOutputStream(dirZip+fileNameZip));
FileInputStream in = new FileInputStream(dirSource+fileName);
out.putNextEntry(new ZipEntry(dirSource+fileName));
int len;
while ((len = in.read(data)) > 0) {
out.write(data, 0, len);
}
in.close();
out.close();
}catch(Exception e){
System.out.println("Fehler: " +e);
}
}
return dirSource+fileName;
}
I haven't tried your solution, but i've managed to find out what was
wrong with my code. Here is what i used to succeded
Date today;
DateFormat dateFormatter;
long systime;
dateFormatter = DateFormat.getDateInstance(DateFormat.DEFAULT,
new Locale("fr","FR"));
today = new Date();
systime = today.getTime();// this was the missing data
try
{
FileOutputStream tempo = new FileOutputStream(temp);
JarOutputStream jarIt = new JarOutputStream(tempo);
jarIt.setMethod(JarOutputStream.STORED);
zipentry = new ZipEntry("foo1.class");
zipentry.setMethod(zipentry.STORED);
zipentry.setSize(save1.length);
zipentry.setTime(systime); // this was the missing data
CRC32 crc321 = new CRC32();
crc321.update(save1);
zipentry.setCrc(crc321.getValue());
jarIt.putNextEntry(zipentry);
jarIt.write(save1);
jarIt.flush();
jarIt.flush();
jarIt.closeEntry();
zipentry = new ZipEntry("foo2.class");
zipentry.setMethod(zipentry.STORED);
zipentry.setTime(systime); // this was the missing data
zipentry.setSize(save2.length);
CRC32 crc322 = new CRC32();
crc322.update(save2);
zipentry.setCrc(crc322.getValue());
jarIt.putNextEntry(zipentry);
jarIt.write(save2);
jarIt.flush();
jarIt.flush();
jarIt.closeEntry();
jarIt.finish();
jarIt.close();
}
catch(java.io.FileNotFoundException fnfe)
{
fnfe.printStackTrace();
}
catch(java.io.IOException ioe)
{
ioe.printStackTrace();
}
Hope this code will help someone