Resume FileOutputStream efter IOException
Programmet skal kunne pakke et antal billeder ned i en zipfil ved hjælp af api\'erne under java.util.zip.*Det virker også fint, men hvis der er mangel på diskplads skal man ha lov til at ryde op og så pakke videre der hvor den er kommet til. Hvorfor springer programmet over nogle bytes når jeg resumer tråden.
Koden:
/*
* @(#)ZipImage.java 1.0 Beta, 05/09/2000
*
* Copyright 2000 BitExtreme.
* All rights reserved.
*/
package ZipImages;
/**
* Packing of multiple images into Zip-file.
*
* @author BitExtreme
* @version 1.0 Beta, 05/09/2000
*/
import java.util.ArrayList;
import java.util.Iterator;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.zip.ZipOutputStream;
import java.util.zip.ZipEntry;
import java.io.BufferedWriter;
import java.io.BufferedOutputStream;
import java.io.FileDescriptor;
import javax.swing.DefaultBoundedRangeModel;
public class ZipImage extends Thread {
/**
* This class pack multiple images into a zip-file, whos name is
* generated from the ordernumber, does not support resume and terminate.
*/
protected ArrayList imageList;
protected int imageCount;
protected String orderNumber;
protected String fileName = null;
protected int numberPacked = 0;
protected boolean finnished = false;
protected File zipFile = null;
protected DefaultBoundedRangeModel brModel = new DefaultBoundedRangeModel();
protected boolean ioRestart = false;
protected FileOutputStream out = null;
protected ZipOutputStream zipOut = null;
protected BufferedOutputStream bufOut = null;
public ZipImage(ArrayList imageList,String orderNumber) {
this.imageList = imageList;
this.orderNumber = orderNumber;
this.generateFilename();
this.imageCount = imageList.size();
}
public void run() {
File fileEntry = null;
FileInputStream fileEntryIn = null;
String name = null;
try {
zipFile = new File(fileName);
zipFile.createNewFile();
out = new FileOutputStream(zipFile);
zipOut = new ZipOutputStream(out);
zipOut.setMethod(ZipOutputStream.DEFLATED); // compress
} catch (java.io.IOException ioExcep) { // fejl
}
// for each file ......
Iterator i = imageList.iterator();
numberPacked = 0;
while (i.hasNext()) {
try {
fileEntry = new File((String)i.next());
name = fileEntry.getName();
ZipEntry zipEntry = new ZipEntry(name);
zipEntry.setSize(fileEntry.length());
zipEntry.setTime(fileEntry.lastModified());
zipOut.putNextEntry(zipEntry);
fileEntryIn = new FileInputStream(fileEntry);
} catch (java.io.FileNotFoundException fileNotFoundExcep) { // fejl
} catch (java.io.IOException ioExcep) { // fejl
}
//System.out.println(\"Start inden Try til BufferedOutputStream\");
try {
bufOut = new BufferedOutputStream(zipOut,1048576);
byte[] buffer = new byte[1048576];
int numberRead;
// læs filen og hæld det i buffer, skriv antallet af bytes i numberRead
//System.out.println(\"Start skrivning, filesize \"+fileEntryIn.available());
//if (ioRestart) { System.out.println(\"While 1\"); }
System.out.println(\"START LOOP: \"+fileEntryIn.available());
boolean pack = true;
while (pack) {
try {
if (ioRestart) {
bufOut.flush();
ioRestart = false;
}
numberRead = fileEntryIn.read(buffer);
if (numberRead >= 0) {
System.out.println(\"IN LOOP: \"+fileEntryIn.available());
// skriv buffer fra 0 til længden ned i zipOut
bufOut.write(buffer,0,numberRead);
} else {
pack = false;
}
} catch (java.util.zip.ZipException zipExcep) { // fejl
// ********* zip exception
System.out.println(\"ZIP EXCEPTION: \"+zipExcep);
this.currentThread().suspend();
} catch (java.io.IOException ioExcep) { // fejl
// *****IO**** skrivefejl (ikke mere plads)
System.out.println(\"IN IOEXCEP: \"+fileEntryIn.available());
System.out.println(\"IO EXCEPTION: \"+ioExcep);
ioRestart = true;
this.currentThread().suspend();
}
} // while
fileEntryIn.close();
zipOut.closeEntry();
numberPacked++;
brModel.setValue(numberPacked*100/imageCount);
} catch (java.io.FileNotFoundException fileNotFoundExcep) { // fejl
} catch (java.io.IOException ioExcep) { // fejl
}
} // while
try {
zipOut.finish();
zipOut.close();
zipFile = null;
} catch (java.io.IOException ioExcep) { // fejl
}
finnished = true;
System.out.println(\"Finished writing zipfile.\");
}
public DefaultBoundedRangeModel getBRModel() {
return brModel;
}
public String getFilename() {
return fileName;
}
public int getNumberPacked() {
return numberPacked;
}
public String getNumberOfTotalPacked() {
return String.valueOf(numberPacked)+\"/\"+String.valueOf(imageCount);
}
public boolean finnishedPacking() {
return finnished;
}
private void generateFilename() {
fileName = \"g:\\\\javatest\\\\\" + orderNumber + \".zip\";
}
}