Hi!
ich habe ein Problem: Ich will ein Zip-Archiv erstellen. Habe nach einiger Zeit frustriertem Rumprogrammieren dann auch auf bereits fertige Quelltexte aus dem Internet (sowohl aus dem JavaCodebook als auch von der Problem beim erstellen von ZIP file - Java @ tutorials.de: Forum, Tutorial, Anleitung, Schulung & Hilfe Seite (unterer Eintrag) zurückgegriffen, aber dort habe ich genau die gleichen Probleme.
Und zwar ist das Zip-Archiv, welches ich bekomme, einfach leer. Es wird erzeugt und zeigt mir zwar vorher an, dass etwa 8 MB drin sein sollten, ist aber leer!
Was tun?
Hier nochmal der 1. Quelltext:
(Pfade natürlich abgekürzt).
ich habe ein Problem: Ich will ein Zip-Archiv erstellen. Habe nach einiger Zeit frustriertem Rumprogrammieren dann auch auf bereits fertige Quelltexte aus dem Internet (sowohl aus dem JavaCodebook als auch von der Problem beim erstellen von ZIP file - Java @ tutorials.de: Forum, Tutorial, Anleitung, Schulung & Hilfe Seite (unterer Eintrag) zurückgegriffen, aber dort habe ich genau die gleichen Probleme.
Und zwar ist das Zip-Archiv, welches ich bekomme, einfach leer. Es wird erzeugt und zeigt mir zwar vorher an, dass etwa 8 MB drin sein sollten, ist aber leer!
Was tun?
Hier nochmal der 1. Quelltext:
Java:
package src;
import java.io.BufferedInputStream;
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;
public class ZipCreator {
private String archiveName;
private ZipOutputStream outputStream = null;
public ZipCreator(String name) {
archiveName = name;
}
public void create() throws FileNotFoundException {
outputStream = new ZipOutputStream(new FileOutputStream(archiveName));
}
public void close() throws IOException {
outputStream.close();
}
public void add(File f) throws IOException {
String name = f.getCanonicalPath();
long len = f.length();
ZipEntry zipEntry = new ZipEntry(name);
zipEntry.setSize(len);
zipEntry.setTime(f.lastModified());
zipEntry.setMethod(ZipEntry.DEFLATED);
FileInputStream fis = new FileInputStream(f);
BufferedInputStream bis = new BufferedInputStream(fis);
outputStream.putNextEntry(zipEntry);
byte[] buffer = new byte[2048];
int num;
while ((num = bis.read(buffer)) >= 0) {
outputStream.write(buffer, 0, num);}
bis.close();
outputStream.closeEntry();
}
public static void main(String[] a) throws IOException {
ZipCreator zip = new ZipCreator(
"C:/Users/.../asdf.zip");
zip.create();
File asd = new File(
"C:/Users/.../Vorlage2.dot");
zip.add(asd);
zip
.add(new File(
"C:/Users/.../final.doc"));
zip.close();
}
}
Java:
package src;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
/**
* @author Tom
*
*/
public class ZipCreator {
static byte[] buffer = new byte[8192];
static int len = 0;
/**
* @param args
*/
public static void main(String[] args) throws Exception {
ZipOutputStream zipOutputStream = new ZipOutputStream(
new FileOutputStream("C:/Users/.../blabla.zip"));
File file = new File("C:/Users/.../ordnername");
treeWalkAndCompressTo(file, zipOutputStream);
zipOutputStream.close();
}
private static void treeWalkAndCompressTo(File file,
ZipOutputStream zipOutputStream) throws Exception {
if (!file.isDirectory()) {
ZipEntry entry = new ZipEntry(file.getAbsolutePath());
zipOutputStream.putNextEntry(entry);
FileInputStream fis = new FileInputStream(file);
while ((len = fis.read(buffer)) > 0) {
zipOutputStream.write(buffer, 0, len);
}
fis.close();
zipOutputStream.closeEntry();
} else {
File[] children = file.listFiles();
for (int i = 0; i < children.length; i++) {
File child = children[i];
treeWalkAndCompressTo(child, zipOutputStream);
}
}
}
}
(Pfade natürlich abgekürzt).