OpenConcerto

Dépôt officiel du code source de l'ERP OpenConcerto
sonarqube

svn://code.openconcerto.org/openconcerto

Rev

Rev 17 | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 17 Rev 142
Line 14... Line 14...
14
 package org.openconcerto.utils;
14
 package org.openconcerto.utils;
15
 
15
 
16
import java.io.BufferedInputStream;
16
import java.io.BufferedInputStream;
17
import java.io.BufferedOutputStream;
17
import java.io.BufferedOutputStream;
18
import java.io.ByteArrayInputStream;
18
import java.io.ByteArrayInputStream;
-
 
19
import java.io.ByteArrayOutputStream;
19
import java.io.File;
20
import java.io.File;
20
import java.io.FileInputStream;
21
import java.io.FileInputStream;
21
import java.io.FileNotFoundException;
22
import java.io.FileNotFoundException;
22
import java.io.FileOutputStream;
23
import java.io.FileOutputStream;
23
import java.io.IOException;
24
import java.io.IOException;
24
import java.io.InputStream;
25
import java.io.InputStream;
25
import java.io.OutputStream;
26
import java.io.OutputStream;
-
 
27
import java.nio.ByteBuffer;
26
import java.util.Enumeration;
28
import java.util.Enumeration;
27
import java.util.Set;
29
import java.util.Set;
28
import java.util.zip.CRC32;
30
import java.util.zip.CRC32;
-
 
31
import java.util.zip.DeflaterOutputStream;
-
 
32
import java.util.zip.InflaterInputStream;
29
import java.util.zip.ZipEntry;
33
import java.util.zip.ZipEntry;
30
import java.util.zip.ZipException;
34
import java.util.zip.ZipException;
31
import java.util.zip.ZipOutputStream;
35
import java.util.zip.ZipOutputStream;
32
 
36
 
33
/**
37
/**
Line 36... Line 40...
36
 * @author ILM Informatique
40
 * @author ILM Informatique
37
 * @see org.openconcerto.utils.Unzip
41
 * @see org.openconcerto.utils.Unzip
38
 */
42
 */
39
public class Zip {
43
public class Zip {
40
 
44
 
-
 
45
    static public byte[] deflate(final String s) throws IOException {
-
 
46
        return deflate(s.getBytes(StringUtils.UTF8));
-
 
47
    }
-
 
48
 
-
 
49
    static public byte[] deflate(final byte[] b) throws IOException {
-
 
50
        final ByteArrayOutputStream bOut = new ByteArrayOutputStream();
-
 
51
        final DeflaterOutputStream out = new DeflaterOutputStream(bOut);
-
 
52
        out.write(b);
-
 
53
        out.close();
-
 
54
        return bOut.toByteArray();
-
 
55
    }
-
 
56
 
-
 
57
    static public ByteBuffer deflateToBuffer(final String s) throws IOException {
-
 
58
        return ByteBuffer.wrap(deflate(s));
-
 
59
    }
-
 
60
 
-
 
61
    static public String inflateToString(final ByteBuffer payload) throws IOException {
-
 
62
        return inflateToString(payload, payload.remaining());
-
 
63
    }
-
 
64
 
-
 
65
    static public String inflateToString(final ByteBuffer payload, final int length) throws IOException {
-
 
66
        final byte[] b = new byte[length];
-
 
67
        payload.get(b);
-
 
68
        return inflateToString(b);
-
 
69
    }
-
 
70
 
-
 
71
    static public String inflateToString(final byte[] b) throws IOException {
-
 
72
        return new String(inflate(b), StringUtils.UTF8);
-
 
73
    }
-
 
74
 
-
 
75
    static public byte[] inflate(final byte[] b) throws IOException {
-
 
76
        final InflaterInputStream in = new InflaterInputStream(new ByteArrayInputStream(b));
-
 
77
        final ByteArrayOutputStream out = new ByteArrayOutputStream();
-
 
78
        StreamUtils.copy(in, out);
-
 
79
        out.close();
-
 
80
        return out.toByteArray();
-
 
81
    }
-
 
82
 
41
    /**
83
    /**
42
     * Copie de from dans to seulement les entrées dont le nom n'est pas dans
84
     * Copie de from dans to seulement les entrées dont le nom n'est pas dans
43
     * <code>excludedEntries</code>.
85
     * <code>excludedEntries</code>.
44
     * 
86
     * 
45
     * @param from le zip source.
87
     * @param from le zip source.
Line 48... Line 90...
48
     *        existantes.
90
     *        existantes.
49
     * @return le fichier zip dest.
91
     * @return le fichier zip dest.
50
     * @throws ZipException
92
     * @throws ZipException
51
     * @throws IOException
93
     * @throws IOException
52
     */
94
     */
53
    static public Zip createFrom(File from, File to, Set excludedEntries) throws ZipException, IOException {
95
    static public Zip createFrom(File from, File to, Set<String> excludedEntries) throws ZipException, IOException {
54
        Unzip unz = new Unzip(from);
96
        Unzip unz = new Unzip(from);
55
        Zip res = new Zip(to);
97
        Zip res = new Zip(to);
56
        final Enumeration en = unz.entries();
98
        final Enumeration<? extends ZipEntry> en = unz.entries();
57
        while (en.hasMoreElements()) {
99
        while (en.hasMoreElements()) {
58
            ZipEntry entry = (ZipEntry) en.nextElement();
100
            final ZipEntry entry = en.nextElement();
59
            if (!excludedEntries.contains(entry.getName())) {
101
            if (!excludedEntries.contains(entry.getName())) {
60
                res.zip(entry.getName(), unz.getInputStream(entry));
102
                res.zip(entry.getName(), unz.getInputStream(entry));
61
            }
103
            }
62
        }
104
        }
63
        unz.close();
105
        unz.close();
Line 71... Line 113...
71
     * @param entriesName les noms des entrées à effacer.
113
     * @param entriesName les noms des entrées à effacer.
72
     * @param dest le zip de destination.
114
     * @param dest le zip de destination.
73
     * @throws ZipException
115
     * @throws ZipException
74
     * @throws IOException
116
     * @throws IOException
75
     */
117
     */
76
    static public void delete(File src, Set entriesName, File dest) throws ZipException, IOException {
118
    static public void delete(File src, Set<String> entriesName, File dest) throws ZipException, IOException {
77
        if (dest.exists())
119
        if (dest.exists())
78
            dest.delete();
120
            dest.delete();
79
        createFrom(src, dest, entriesName).close();
121
        createFrom(src, dest, entriesName).close();
80
    }
122
    }
81
 
123