external artifact builder: preserve compression method for entries extracted from one archive and packed into another (to fix problems with Android *.apk artifacts)

This commit is contained in:
nik
2012-12-25 14:24:03 +04:00
parent 5a94cf14c0
commit e5b70c1092
3 changed files with 57 additions and 10 deletions
@@ -246,7 +246,7 @@ public class JarsBuilder {
final Ref<Manifest> manifestRef = Ref.create(null);
((JarBasedArtifactRootDescriptor)descriptor).processEntries(new JarBasedArtifactRootDescriptor.EntryProcessor() {
@Override
public void process(@Nullable InputStream inputStream, @NotNull String relativePath) throws IOException {
public void process(@Nullable InputStream inputStream, @NotNull String relativePath, ZipEntry entry) throws IOException {
if (manifestRef.isNull() && relativePath.equals(manifestPath) && inputStream != null) {
manifestRef.set(createManifest(inputStream, descriptor.getRootFile()));
}
@@ -285,7 +285,7 @@ public class JarsBuilder {
final long timestamp = FileSystemUtil.lastModified(root.getRootFile());
root.processEntries(new JarBasedArtifactRootDescriptor.EntryProcessor() {
@Override
public void process(@Nullable InputStream inputStream, @NotNull String relativePath) throws IOException {
public void process(@Nullable InputStream inputStream, @NotNull String relativePath, ZipEntry entry) throws IOException {
String pathInJar = addParentDirectories(jarOutputStream, writtenPaths, JpsArtifactPathUtil
.appendToPath(relativeOutputPath, relativePath));
@@ -293,9 +293,14 @@ public class JarsBuilder {
addDirectoryEntry(jarOutputStream, pathInJar + "/", writtenPaths);
}
else if (writtenPaths.add(pathInJar)) {
ZipEntry entry = new ZipEntry(pathInJar);
entry.setTime(timestamp);
jarOutputStream.putNextEntry(entry);
ZipEntry newEntry = new ZipEntry(pathInJar);
newEntry.setTime(timestamp);
if (entry.getMethod() == ZipEntry.STORED) {
newEntry.setMethod(ZipEntry.STORED);
newEntry.setSize(entry.getSize());
newEntry.setCrc(entry.getCrc());
}
jarOutputStream.putNextEntry(newEntry);
FileUtil.copy(inputStream, jarOutputStream);
jarOutputStream.closeEntry();
}
@@ -65,7 +65,7 @@ public class JarBasedArtifactRootDescriptor extends ArtifactRootDescriptor {
final String name = entry.getName();
if (name.startsWith(prefix)) {
String relativePath = name.substring(prefix.length());
processor.process(entry.isDirectory() ? null : zipFile.getInputStream(entry), relativePath);
processor.process(entry.isDirectory() ? null : zipFile.getInputStream(entry), relativePath, entry);
}
}
}
@@ -90,7 +90,7 @@ public class JarBasedArtifactRootDescriptor extends ArtifactRootDescriptor {
}
processEntries(new EntryProcessor() {
@Override
public void process(@Nullable InputStream inputStream, @NotNull String relativePath) throws IOException {
public void process(@Nullable InputStream inputStream, @NotNull String relativePath, ZipEntry entry) throws IOException {
final String fullOutputPath = FileUtil.toSystemDependentName(JpsArtifactPathUtil.appendToPath(outputPath, relativePath));
final File outputFile = new File(fullOutputPath);
@@ -119,6 +119,6 @@ public class JarBasedArtifactRootDescriptor extends ArtifactRootDescriptor {
}
public interface EntryProcessor {
void process(@Nullable InputStream inputStream, @NotNull String relativePath) throws IOException;
void process(@Nullable InputStream inputStream, @NotNull String relativePath, ZipEntry entry) throws IOException;
}
}
@@ -23,11 +23,17 @@ import org.jetbrains.jps.model.library.JpsLibrary;
import org.jetbrains.jps.model.module.JpsModule;
import org.jetbrains.jps.util.JpsPathUtil;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Enumeration;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import java.util.zip.CRC32;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipOutputStream;
import static com.intellij.util.io.TestFileSystemBuilder.fs;
import static org.jetbrains.jps.incremental.artifacts.LayoutElementTestUtil.archive;
@@ -294,8 +300,7 @@ public class ArtifactBuilderTest extends ArtifactBuilderTestCase {
.fileCopy(firstFile).fileCopy(manifestFile).fileCopy(lastFile));
buildArtifacts(a);
final String jarPath = a.getOutputPath() + "/a.jar";
assertNotNull(jarPath);
JarFile jarFile = new JarFile(new File(FileUtil.toSystemDependentName(jarPath)));
JarFile jarFile = new JarFile(new File(jarPath));
try {
final Enumeration<JarEntry> entries = jarFile.entries();
assertTrue(entries.hasMoreElements());
@@ -307,6 +312,43 @@ public class ArtifactBuilderTest extends ArtifactBuilderTestCase {
}
}
public void testPreserveCompressionMethodForEntryExtractedFromOneArchiveAndPackedIntoAnother() throws IOException {
String path = createFile("data/a.jar");
ZipOutputStream output = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(new File(path))));
try {
ZipEntry entry = new ZipEntry("a.txt");
byte[] text = "text".getBytes();
entry.setMethod(ZipEntry.STORED);
entry.setSize(text.length);
CRC32 crc32 = new CRC32();
crc32.update(text);
entry.setCrc(crc32.getValue());
output.putNextEntry(entry);
output.write(text);
output.closeEntry();
}
catch (Exception e) {
e.printStackTrace();
}
finally {
output.close();
}
JpsArtifact a = addArtifact(archive("b.jar").extractedDir(path, ""));
buildAll();
assertOutput(a, fs().archive("b.jar").file("a.txt", "text"));
final String jarPath = a.getOutputPath() + "/b.jar";
ZipFile zipFile = new ZipFile(new File(jarPath));
try {
ZipEntry entry = zipFile.getEntry("a.txt");
assertNotNull(entry);
assertEquals(ZipEntry.STORED, entry.getMethod());
}
finally {
zipFile.close();
}
}
public void testBuildModuleBeforeArtifactIfSomeDirectoryInsideModuleOutputIsCopiedToArtifact() {
String src = PathUtil.getParentPath(PathUtil.getParentPath(createFile("src/x/A.java", "package x; class A{}")));
JpsModule module = addModule("m", src);