TarArchiver.java 3.7 KB
Newer Older
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
/*
 * The MIT License
 *
 * Copyright (c) 2010, InfraDNA, Inc.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

package hudson.util.io;

import hudson.Functions;
import hudson.org.apache.tools.tar.TarOutputStream;
29
import hudson.os.PosixException;
30
import hudson.util.FileVisitor;
31
import hudson.util.IOUtils;
32 33 34 35 36 37 38
import org.apache.tools.tar.TarEntry;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
K
kohsuke 已提交
39
import java.lang.reflect.Field;
40 41
import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
import org.apache.commons.compress.archivers.tar.TarArchiveOutputStream;
K
kohsuke 已提交
42 43

import static org.apache.tools.tar.TarConstants.LF_SYMLINK;
44 45 46 47 48 49 50 51

/**
 * {@link FileVisitor} that creates a tar archive.
 *
 * @see ArchiverFactory#TAR
 */
final class TarArchiver extends Archiver {
    private final byte[] buf = new byte[8192];
52
    private final TarArchiveOutputStream tar;
53 54

    TarArchiver(OutputStream out) {
55
        tar = new TarArchiveOutputStream(out);
56
        tar.setBigNumberMode(TarArchiveOutputStream.BIGNUMBER_STAR);
57
        tar.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU);
58 59
    }

K
kohsuke 已提交
60 61
    @Override
    public void visitSymlink(File link, String target, String relativePath) throws IOException {
62
        TarArchiveEntry e = new TarArchiveEntry(relativePath, LF_SYMLINK);
63 64 65 66 67 68 69 70
        try {
            int mode = IOUtils.mode(link);
            if (mode != -1) {
                e.setMode(mode);
            }
        } catch (PosixException x) {
            // ignore
        }
71 72
        
        e.setLinkName(target);
K
kohsuke 已提交
73

74 75
        tar.putArchiveEntry(e);
        tar.closeArchiveEntry();
K
kohsuke 已提交
76 77 78 79 80 81 82 83
        entriesWritten++;
    }

    @Override
    public boolean understandsSymlink() {
        return true;
    }

84 85 86 87 88 89
    public void visit(File file, String relativePath) throws IOException {
        if(Functions.isWindows())
            relativePath = relativePath.replace('\\','/');

        if(file.isDirectory())
            relativePath+='/';
90
        TarArchiveEntry te = new TarArchiveEntry(relativePath);
91 92
        int mode = IOUtils.mode(file);
        if (mode!=-1)   te.setMode(mode);
93 94 95 96
        te.setModTime(file.lastModified());
        if(!file.isDirectory())
            te.setSize(file.length());

97
        tar.putArchiveEntry(te);
98 99 100 101 102 103 104 105 106 107 108 109

        if (!file.isDirectory()) {
            FileInputStream in = new FileInputStream(file);
            try {
                int len;
                while((len=in.read(buf))>=0)
                    tar.write(buf,0,len);
            } finally {
                in.close();
            }
        }

110
        tar.closeArchiveEntry();
111 112 113 114 115 116 117
        entriesWritten++;
    }

    public void close() throws IOException {
        tar.close();
    }
}