File D:\maven-qcr-plugin-testsunitaires\qcr4java\jenkins-master\target\site\qc\core\src\main\java\hudson\util\io\ZipArchiver.java.html

1 /*
2 * The MIT License
3 *
4 * Copyright (c) 2010, InfraDNA, Inc.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
24
25 package hudson.util.io;
26
27 import hudson.util.FileVisitor;
28 import hudson.util.IOUtils;
29 import org.apache.tools.zip.ZipEntry;
30 import org.apache.tools.zip.ZipOutputStream;
31
32 import java.io.File;
33 import java.io.FileInputStream;
34 import java.io.IOException;
35 import java.io.OutputStream;
36
37 /**
38 * {@link FileVisitor} that creates a zip archive.
39 *
40 * @see ArchiverFactory#ZIP
41 */
42 final class ZipArchiver extends Archiver {
43 private final byte[] buf = new byte[8192];
44 private final ZipOutputStream zip;
45
46 ZipArchiver(OutputStream out) {
47 zip = new ZipOutputStream(out);
48 zip.setEncoding(System.getProperty("file.encoding"));
49 }
50
51 public void visit(final File f, final String _relativePath) throws IOException {
52 int mode = IOUtils.mode(f);
53
54 // On Windows, the elements of relativePath are separated by
55 // back-slashes (\), but Zip files need to have their path elements separated
56 // by forward-slashes (/)
57 String relativePath = _relativePath.replace('\\', '/');
58
59 if(f.isDirectory()) {
60 ZipEntry dirZipEntry = new ZipEntry(relativePath+'/');
61 // Setting this bit explicitly is needed by some unzipping applications (see JENKINS-3294).
62 dirZipEntry.setExternalAttributes(BITMASK_IS_DIRECTORY);
63 if (mode!=-1) dirZipEntry.setUnixMode(mode);
64 dirZipEntry.setTime(f.lastModified());
65 zip.putNextEntry(dirZipEntry);
66 zip.closeEntry();
67 } else {
68 ZipEntry fileZipEntry = new ZipEntry(relativePath);
69 if (mode!=-1) fileZipEntry.setUnixMode(mode);
70 fileZipEntry.setTime(f.lastModified());
71 zip.putNextEntry(fileZipEntry);
72 FileInputStream in = new FileInputStream(f);
73 int len;
74 while((len=in.read(buf))>0)
75 zip.write(buf,0,len);
76 in.close();
77 zip.closeEntry();
78 }
79 entriesWritten++;
80 }
81
82 public void close() throws IOException {
83 zip.close();
84 }
85
86 // Bitmask indicating directories in 'external attributes' of a ZIP archive entry.
87 private static final long BITMASK_IS_DIRECTORY = 1<<4;
88 }
89