Davy83
18-05-2012, 13:07
Ciao a tutti,
il mio problema riguarda la compattazione di file su Linux con la libreria java.util.zip.*;
La compattazione è ok, solo che i file al suo interno non hanno permessi in lettura, sono così: -rw------- invece che -rw-r--r--
E quindi un'eventuale rilettura, sempre tramite java e la classe Zip è impossibile.
Il metodo da me usato è il seguente:
public static void zipFiles(String srcDir, String zipName) throws FileNotFoundException, IOException {
File output = new File(zipName);
ZipOutputStream zip = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(zipName)));
File dir = new File(srcDir);
File[] files = dir.listFiles();
for (File f : files) {
f.setReadable(true);
f.setWritable(true);
logger.info(f.getName());
if (f.isDirectory()) {
addFolder(f, f.getName(), zip);
} else {
addFile(f, "", zip);
}
}
zip.flush();
zip.finish();
zip.close();
}
private static void addFolder(File dir, String path, ZipOutputStream zip) throws FileNotFoundException, IOException {
dir.mkdir();
File[] files = dir.listFiles();
for (File f : files) {
addFile(f, path, zip);
}
}
private static void addFile(File f, String path, ZipOutputStream zip) throws FileNotFoundException, IOException {
if (f.isDirectory()) {
f.mkdir();
addFolder(f, path + File.separator + f.getName(), zip);
} else {
//byte[] buf = new byte[2048];
byte[] buf = f.toString().getBytes();
int len;
// FileInputStream in = new FileInputStream(f);
ZipEntry entry = new ZipEntry(path + File.separator + f.getName());
zip.putNextEntry(entry);
// while ((len = in.read(buf)) > 0) {
// zip.write(buf, 0, len);
zip.write(buf);
// }
}
logger.info("ADDedToZip"+path + File.separator + f.getName());
}
il mio problema riguarda la compattazione di file su Linux con la libreria java.util.zip.*;
La compattazione è ok, solo che i file al suo interno non hanno permessi in lettura, sono così: -rw------- invece che -rw-r--r--
E quindi un'eventuale rilettura, sempre tramite java e la classe Zip è impossibile.
Il metodo da me usato è il seguente:
public static void zipFiles(String srcDir, String zipName) throws FileNotFoundException, IOException {
File output = new File(zipName);
ZipOutputStream zip = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(zipName)));
File dir = new File(srcDir);
File[] files = dir.listFiles();
for (File f : files) {
f.setReadable(true);
f.setWritable(true);
logger.info(f.getName());
if (f.isDirectory()) {
addFolder(f, f.getName(), zip);
} else {
addFile(f, "", zip);
}
}
zip.flush();
zip.finish();
zip.close();
}
private static void addFolder(File dir, String path, ZipOutputStream zip) throws FileNotFoundException, IOException {
dir.mkdir();
File[] files = dir.listFiles();
for (File f : files) {
addFile(f, path, zip);
}
}
private static void addFile(File f, String path, ZipOutputStream zip) throws FileNotFoundException, IOException {
if (f.isDirectory()) {
f.mkdir();
addFolder(f, path + File.separator + f.getName(), zip);
} else {
//byte[] buf = new byte[2048];
byte[] buf = f.toString().getBytes();
int len;
// FileInputStream in = new FileInputStream(f);
ZipEntry entry = new ZipEntry(path + File.separator + f.getName());
zip.putNextEntry(entry);
// while ((len = in.read(buf)) > 0) {
// zip.write(buf, 0, len);
zip.write(buf);
// }
}
logger.info("ADDedToZip"+path + File.separator + f.getName());
}