|
23 | 23 | import java.io.PrintStream;
|
24 | 24 | import java.lang.reflect.Method;
|
25 | 25 | import java.nio.file.Files;
|
| 26 | +import java.nio.file.LinkOption; |
26 | 27 | import java.nio.file.Path;
|
27 | 28 | import java.nio.file.StandardCopyOption;
|
| 29 | +import java.nio.file.attribute.FileAttribute; |
28 | 30 | import java.nio.file.attribute.FileTime;
|
| 31 | +import java.nio.file.attribute.PosixFileAttributeView; |
| 32 | +import java.nio.file.attribute.PosixFileAttributes; |
| 33 | +import java.nio.file.attribute.PosixFilePermissions; |
29 | 34 | import java.util.ArrayList;
|
30 | 35 | import java.util.Calendar;
|
31 | 36 | import java.util.Enumeration;
|
@@ -147,23 +152,43 @@ protected void postCreateArchive() throws ArchiverException {
|
147 | 152 | private void fixLastModifiedTimeZipEntries() throws IOException {
|
148 | 153 | long timeMillis = getLastModifiedTime().toMillis();
|
149 | 154 | Path destFile = getDestFile().toPath();
|
150 |
| - Path tmpZip = Files.createTempFile(destFile.getParent(), null, null); |
151 |
| - try (ZipFile zipFile = new ZipFile(getDestFile()); |
152 |
| - ZipOutputStream out = new ZipOutputStream(Files.newOutputStream(tmpZip))) { |
153 |
| - Enumeration<? extends ZipEntry> entries = zipFile.entries(); |
154 |
| - while (entries.hasMoreElements()) { |
155 |
| - ZipEntry entry = entries.nextElement(); |
156 |
| - // Not using setLastModifiedTime(FileTime) as it sets the extended timestamp |
157 |
| - // which is not compatible with the jar tool output. |
158 |
| - entry.setTime(timeMillis); |
159 |
| - out.putNextEntry(entry); |
160 |
| - if (!entry.isDirectory()) { |
161 |
| - IOUtil.copy(zipFile.getInputStream(entry), out); |
| 155 | + PosixFileAttributes posixFileAttributes = Files.getFileAttributeView( |
| 156 | + destFile, PosixFileAttributeView.class, LinkOption.NOFOLLOW_LINKS) |
| 157 | + .readAttributes(); |
| 158 | + FileAttribute<?>[] attributes; |
| 159 | + if (posixFileAttributes != null) { |
| 160 | + attributes = new FileAttribute<?>[1]; |
| 161 | + attributes[0] = PosixFilePermissions.asFileAttribute(posixFileAttributes.permissions()); |
| 162 | + } else { |
| 163 | + attributes = new FileAttribute<?>[0]; |
| 164 | + } |
| 165 | + Path tmpZip = Files.createTempFile(destFile.getParent(), null, null, attributes); |
| 166 | + try { |
| 167 | + try (ZipFile zipFile = new ZipFile(getDestFile()); |
| 168 | + ZipOutputStream out = new ZipOutputStream(Files.newOutputStream(tmpZip))) { |
| 169 | + Enumeration<? extends ZipEntry> entries = zipFile.entries(); |
| 170 | + while (entries.hasMoreElements()) { |
| 171 | + ZipEntry entry = entries.nextElement(); |
| 172 | + // Not using setLastModifiedTime(FileTime) as it sets the extended timestamp |
| 173 | + // which is not compatible with the jar tool output. |
| 174 | + entry.setTime(timeMillis); |
| 175 | + out.putNextEntry(entry); |
| 176 | + if (!entry.isDirectory()) { |
| 177 | + IOUtil.copy(zipFile.getInputStream(entry), out); |
| 178 | + } |
| 179 | + out.closeEntry(); |
162 | 180 | }
|
163 |
| - out.closeEntry(); |
164 | 181 | }
|
| 182 | + Files.move(tmpZip, destFile, StandardCopyOption.REPLACE_EXISTING); |
| 183 | + } catch (IOException e) { |
| 184 | + // Clean up temporary file if an error occurs |
| 185 | + try { |
| 186 | + Files.delete(tmpZip); |
| 187 | + } catch (IOException ioe) { |
| 188 | + e.addSuppressed(ioe); |
| 189 | + } |
| 190 | + throw e; |
165 | 191 | }
|
166 |
| - Files.move(tmpZip, destFile, StandardCopyOption.REPLACE_EXISTING); |
167 | 192 | }
|
168 | 193 |
|
169 | 194 | /**
|
|
0 commit comments