Skip to content

Commit

Permalink
[JENKINS-28862][JENKINS-27042] Perform archiving using truezip via on…
Browse files Browse the repository at this point in the history
…e remoting roundtrip.

JENKINS-28862 was resolved so it introduced a regression for JENKINS-27042
in 1.6. Now both should be resolved implementing hudson.util.io.Archiver,
based on truezip.
  • Loading branch information
olivergondza committed Jun 21, 2015
1 parent 59bf4a8 commit 6f6932c
Show file tree
Hide file tree
Showing 2 changed files with 114 additions and 21 deletions.
@@ -0,0 +1,106 @@
/*
* 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 org.jenkinsci.plugins.compress_artifacts;

import hudson.util.FileVisitor;
import hudson.util.IOUtils;
import hudson.util.io.Archiver;
import hudson.util.io.ArchiverFactory;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.charset.Charset;

import de.schlichtherle.truezip.zip.ZipEntry;
import de.schlichtherle.truezip.zip.ZipOutputStream;

/**
* {@link FileVisitor} that creates a zip archive via TrueZip.
*
* Modified version of {@link hudson.util.io.ZipArchiver} that created archives
* larger than 4G successfully.
*/
final /*pacakge*/ class TrueZipArchiver extends Archiver {
private final byte[] buf = new byte[8192];
private final ZipOutputStream zip;

/*package*/ TrueZipArchiver(OutputStream out) {
zip = new ZipOutputStream(out, Charset.defaultCharset());
}

@Override
public void visit(final File f, final String _relativePath) throws IOException {
// int mode = IOUtils.mode(f); // TODO

// On Windows, the elements of relativePath are separated by
// back-slashes (\), but Zip files need to have their path elements separated
// by forward-slashes (/)
String relativePath = _relativePath.replace('\\', '/');

if(f.isDirectory()) {
ZipEntry dirZipEntry = new ZipEntry(relativePath+'/');
// Setting this bit explicitly is needed by some unzipping applications (see JENKINS-3294).
dirZipEntry.setExternalAttributes(BITMASK_IS_DIRECTORY);
//if (mode!=-1) dirZipEntry.setUnixMode(mode); // TODO
dirZipEntry.setTime(f.lastModified());
zip.putNextEntry(dirZipEntry);
zip.closeEntry();
} else {
ZipEntry fileZipEntry = new ZipEntry(relativePath);
//if (mode!=-1) fileZipEntry.setUnixMode(mode); // TODO
fileZipEntry.setTime(f.lastModified());
zip.putNextEntry(fileZipEntry);
FileInputStream in = new FileInputStream(f);
try {
int len;
while((len=in.read(buf))>=0)
zip.write(buf,0,len);
} finally {
in.close();
}
zip.closeEntry();
}
entriesWritten++;
}

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

// Bitmask indicating directories in 'external attributes' of a ZIP archive entry.
private static final long BITMASK_IS_DIRECTORY = 1<<4;

public static final ArchiverFactory FACTORY = new Factory();

private static final class Factory extends ArchiverFactory {
private static final long serialVersionUID = 1L;
@Override
public Archiver create(OutputStream out) throws IOException {
return new TrueZipArchiver(out);
}
};
}
Expand Up @@ -30,16 +30,16 @@

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FilterInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.charset.Charset;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import javax.annotation.Nonnull;
Expand All @@ -50,19 +50,11 @@
import org.apache.commons.httpclient.util.URIUtil;
import org.apache.tools.ant.types.selectors.SelectorUtils;

import de.schlichtherle.truezip.file.TArchiveDetector;
import de.schlichtherle.truezip.file.TFile;
import de.schlichtherle.truezip.file.TVFS;
import de.schlichtherle.truezip.fs.archive.zip.JarDriver;
import de.schlichtherle.truezip.socket.sl.IOPoolLocator;
import de.schlichtherle.truezip.zip.ZipEntry;
import de.schlichtherle.truezip.zip.ZipFile;

final class ZipStorage extends VirtualFile {

// JarDriver is a ZipDriver that uses UTF-8 for entry names
private static final TArchiveDetector DETECTOR = new TArchiveDetector("zip", new JarDriver(IOPoolLocator.SINGLETON));

static VirtualFile root(File archive) {
return new ZipStorage(archive, "");
}
Expand All @@ -72,18 +64,13 @@ static void archive(File archive, FilePath workspace, Launcher launcher, BuildLi
// Use temporary file for writing, rename when done
File tempArchive = new File(archive.getAbsolutePath() + ".writing.zip");

TFile zip = new TFile(tempArchive, DETECTOR);
zip.mkdir(); // Create new archive file
for (Entry<String, String> afs: artifacts.entrySet()) {
FilePath src = workspace.child(afs.getKey());
TFile dst = new TFile(zip, afs.getValue(), TArchiveDetector.NULL);
if (src.isDirectory()) {
dst.mkdirs();
} else {
TFile.cp(src.read(), dst);
}
OutputStream os = new FileOutputStream(tempArchive);
try {
workspace.archive(TrueZipArchiver.FACTORY, os, new FilePath.ExplicitlySpecifiedDirScanner(artifacts));
} finally {
os.close();
}
TVFS.umount(zip);

tempArchive.renameTo(archive);
}

Expand Down

0 comments on commit 6f6932c

Please sign in to comment.