Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

change unzip reading method for byte to byte instead of chars #11

Merged
merged 2 commits into from
Feb 18, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,15 @@

import javax.inject.Inject;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.nio.charset.Charset;
import java.util.Enumeration;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import java.util.zip.ZipEntry;
Expand Down Expand Up @@ -144,17 +147,12 @@ public Map<String, String> invoke(File zipFile, VirtualChannel channel) throws I
} else {
logger.print("Reading: ");
logger.println(entry.getName());
try (BufferedReader reader = new BufferedReader(new InputStreamReader(zip.getInputStream(entry), Charset.defaultCharset()))) {
String line = reader.readLine();
StringBuilder str = new StringBuilder();
while (line != null) {
if (str.length() >= 0) {
str.append('\n');
}
str.append(line);
line = reader.readLine();
}
strMap.put(entry.getName(), str.toString().trim());
// we need to copy byte by byte everything to be sure that no carriage return characters are skipped
// readLine skips the carriage return and for example files ending with only one carriage return are trimmed

try (InputStream is = zip.getInputStream(entry); ByteArrayOutputStream output = new ByteArrayOutputStream()) {
IOUtils.copyLarge(is, output);
strMap.put(entry.getName(), new String(output.toByteArray(), Charset.defaultCharset()));

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think Charset.defaultCharset() can cause problems in some setups, ZipFile is using UTF8 internally.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You think it might be better to fix to UTF-8? I have never worked with ZipFiles so I used the same approach used previously. What I have read from JavaDoc is that The UTF-8 charset is used to decode the entry names and comments. so it seems that the content itself would be in the original encode right?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the contents of a File - those can be in any Charset under the sun - or even binary - of note is that the Manifest will be in UTF-8.
perhaps this step needs to take an option String charset parameter.

}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,24 @@ public void configRoundTrip() throws Exception {
j.assertEqualDataBoundBeans(step, step2);
}

@Test
public void testJarWithGradleManifest() throws Exception {
URL resource = getClass().getResource("gradle-manifest.war");

String remoting = new File(resource.getPath()).getAbsolutePath();
p.setDefinition(new CpsFlowDefinition(
"node('slaves') {\n" +
" def man = readManifest file: '" + remoting + "'\n" +
" assert man != null\n" +
" assert man.main != null\n" +
" echo man.main['Implementation-Version']\n" +
" assert man.main['Implementation-Version'] == '1.0'\n" +
"}\n"
, false));
WorkflowRun run = j.assertBuildStatusSuccess(p.scheduleBuild2(0));
j.assertLogContains("Reading: META-INF/MANIFEST.MF", run);
}

@Test
public void testJar() throws Exception {
String remoting = new File(j.getWebAppRoot(), "WEB-INF/remoting.jar").getAbsolutePath();
Expand Down
Binary file not shown.