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 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
|
/**
* @author : Anthony
*
* _zos est le fichier Zip de la sauvegarde du jour
****************************/
public class CompressionInc
{
private ZipOutputStream _zos;
/**
* creation du fichier de sortie _zos avec le nommage
* le fichier est stocke dans sauvegarde/zip/incrementale/szDirName
*
*************************************************/
public CompressionInc (String szDir, long now)
throws IOException
{
String szDirName = szDir.replaceAll (":\\\\", "_");
szDirName = szDirName.replaceAll ( "\\\\", "_");
szDirName = szDirName.replaceAll ( "/" , "_");
System.out.println (szDirName);
Date date = new Date ();
date.setTime (now + 1000);
System.out.println (date);
SimpleDateFormat dateFormat = new SimpleDateFormat ("yyyy-MM-dd_HH.mm.ss");
String szDate = dateFormat.format (date);
String szZipFileName = szDirName + szDate + ".zip";
File zipFile = new File ("zip/incrementale/" + szDate + "/" + szZipFileName);
zipFile.getParentFile ().mkdirs ();
FileOutputStream fos = new FileOutputStream (zipFile);
_zos = new ZipOutputStream (fos);
_zos.setLevel (Deflater.DEFAULT_COMPRESSION);
}
/**
* ajoute un fichier a l'archive
* iSize est le nombre de bytes lu pendant la compression
*****************************************************/
public void addFile (String szPath, int iDirLength)
throws FileNotFoundException, IOException, ZipException
{
int iSize = 0;
final int BUFFER = 1024;
byte [] buffer = new byte [BUFFER];
try
{
String szPathFile = szPath.substring (iDirLength);
File fileToCompress = new File (szPath);
if (fileToCompress.isDirectory () == true)
{
ZipEntry zipEntry = new ZipEntry (szPathFile + "/");
_zos.putNextEntry (zipEntry);
System.out.println ("COPIE DU REPERTOIRE " + szPathFile + "/");
}
else
{
File file = new File (szPath);
FileInputStream fileInput = new FileInputStream (file);
BufferedInputStream buffIn = new BufferedInputStream (fileInput);
ZipEntry zipEntry = new ZipEntry (szPathFile);
_zos.putNextEntry (zipEntry);
while ((iSize = buffIn.read (buffer, 0, BUFFER)) != -1)
{
_zos.write (buffer, 0, iSize);
}
_zos.flush ();
_zos.closeEntry ();
buffIn.close ();
System.out.println ("COPIE DE " + szPathFile);
}
}
finally
{
}
}
/**
* Fermer le zip
************************/
public void closeZip ()
throws IOException
{
_zos.finish ();
_zos.close ();
}
} |
Partager