Hi!
Ich habe ein Programm geschrieben, dass die Firefox-Profile exportiert und zippt.
Mein Problem ist, dass ich zwar den kopierten Ordner erhalte, aber keine zip-Datei erstellt wird.
Ich bekomme folgende Exception:
java.io.FileNotFoundException: C:\Users\Andreas\AppData\Local\Temp\MozillaFirefox\Profiles (Zugriff verweigert)
Warum kann ich auf den kopierten Ordner nicht zugreifen und die Datei zippen?
Ich habe ein Programm geschrieben, dass die Firefox-Profile exportiert und zippt.
Mein Problem ist, dass ich zwar den kopierten Ordner erhalte, aber keine zip-Datei erstellt wird.
Ich bekomme folgende Exception:
java.io.FileNotFoundException: C:\Users\Andreas\AppData\Local\Temp\MozillaFirefox\Profiles (Zugriff verweigert)
Warum kann ich auf den kopierten Ordner nicht zugreifen und die Datei zippen?
Java:
import java.io.*;
import java.util.zip.*;
public class Main {
public static void main(String[] args) {
String profile = System.getenv("APPDATA") + "\\Mozilla\\Firefox\\Profiles";
String dest = System.getenv("TEMP") + "\\MozillaFirefox\\Profiles";
String zipfile = System.getenv("TEMP") + "\\MozillaFirefox\\profiles.zip";
try {
File profileDir = new File(profile);
File destDir = new File(dest);
copyDirectory(profileDir, destDir);
ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(destDir));
zip(zipfile, zos);
} catch (IOException e) {
e.printStackTrace();
}
}
public static void copyDirectory(File source, File target) {
File[] files = source.listFiles();
File newFile = null;
target.mkdirs();
if (files != null) {
for (File file : files) {
newFile = new File(target.getAbsolutePath() + "\\" + file.getName());
if (file.isDirectory()) {
copyDirectory(file, newFile);
} else {
copyFile(file, newFile);
}
}
}
}
public static void copyFile(File source, File target) {
try {
BufferedInputStream in = new BufferedInputStream(new FileInputStream(source));
BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(target, true));
int bytes = 0;
while ((bytes = in.read()) != -1) {
out.write(bytes);
}
in.close();
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void zip(String dir2zip, ZipOutputStream zos) {
try {
File zipDir = new File(dir2zip);
String[] dirList = zipDir.list();
byte[] readBuffer = new byte[2156];
int bytesIn = 0;
for (int i = 0; i < dirList.length; i++) {
File f = new File(zipDir, dirList[i]);
if (f.isDirectory()) {
String filePath = f.getPath();
zip(filePath, zos);
continue;
}
FileInputStream fis = new FileInputStream(f);
ZipEntry anEntry = new ZipEntry(f.getPath());
zos.putNextEntry(anEntry);
while ((bytesIn = fis.read(readBuffer)) != -1) {
zos.write(readBuffer, 0, bytesIn);
}
fis.close();
}
zos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}