[Zip] Zip de répertoire ?
Bonjour,
JE cherche à zipper une arborescence de répertoires
J'ai le code suivant pour zipper un répertoire le pb c'est que dans mon archive les répertoires ont leur chemin depuis la racine c ce qui donne en extrayant le fichier dans un répertoire :
Zippeur/c_/temp/rep
Alors que je souhaiterais :
Zippeur/rep/...
Des idées ?
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
| import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
public class Zippeur {
public static void main(String[] args) {
try
{
//create a ZipOutputStream to zip the data to
ZipOutputStream zos = new
ZipOutputStream(new FileOutputStream("c:\\temp\\zippeur.zip"));
//assuming that there is a directory named inFolder (If there
//isn't create one) in the same directory as the one the code
//runs from,
//call the zipDir method
zipDir("c:\\temp\\rep", zos);
//close the stream
zos.close();
}
catch(Exception e)
{
//handle exception
}
}
// here is the code for the method
static void zipDir(String dir2zip, ZipOutputStream zos)
{
try
{
//create a new File object based on the directory we
//have to zip File
File zipDir = new File(dir2zip);
//get a listing of the directory content
String[] dirList = zipDir.list();
byte[] readBuffer = new byte[2156];
int bytesIn = 0;
//loop through dirList, and zip the files
for(int i=0; i<dirList.length; i++)
{
File f = new File(zipDir, dirList[i]);
if(f.isDirectory())
{
//if the File object is a directory, call this
//function again to add its content recursively
String filePath = f.getPath();
zipDir(filePath, zos);
//loop again
continue;
}
//if we reached here, the File object f was not
//a directory
//create a FileInputStream on top of f
FileInputStream fis = new FileInputStream(f);
//create a new zip entry
ZipEntry anEntry = new ZipEntry(f.getPath());
//place the zip entry in the ZipOutputStream object
zos.putNextEntry(anEntry);
//now write the content of the file to the ZipOutputStream
while((bytesIn = fis.read(readBuffer)) != -1)
{
zos.write(readBuffer, 0, bytesIn);
}
//close the Stream
fis.close();
}
}
catch(Exception e)
{
//handle exception
}
}
} |
[Modéré par Didier] : ajout de tag dans le titre - Les règles du forum Java