Changeset 14724 in josm
- Timestamp:
- 2019-01-23T22:20:33+01:00 (6 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/io/session/SessionReader.java
r14201 r14724 13 13 import java.net.URI; 14 14 import java.net.URISyntaxException; 15 import java.nio.charset.StandardCharsets; 15 import java.nio.file.FileSystem; 16 import java.nio.file.FileSystems; 16 17 import java.nio.file.Files; 18 import java.nio.file.Path; 17 19 import java.util.ArrayList; 18 20 import java.util.Collection; 19 21 import java.util.Collections; 20 import java.util.Enumeration;21 22 import java.util.HashMap; 22 23 import java.util.List; … … 24 25 import java.util.Map.Entry; 25 26 import java.util.TreeMap; 26 import java.util.zip.ZipEntry; 27 import java.util.zip.ZipException; 28 import java.util.zip.ZipFile; 27 import java.util.stream.Stream; 29 28 30 29 import javax.swing.JOptionPane; … … 151 150 private static final Map<String, Class<? extends SessionLayerImporter>> sessionLayerImporters = new HashMap<>(); 152 151 153 private URI sessionFileURI;152 private Path sessionFile; 154 153 private boolean zip; // true, if session file is a .joz file; false if it is a .jos file 155 private ZipFilezipFile;154 private FileSystem zipFile; 156 155 private List<Layer> layers = new ArrayList<>(); 157 156 private int active = -1; … … 292 291 * - file inside zip archive: 293 292 * "layers/01/data.osm" 294 * - relativ to the .joz file:293 * - relative to the .joz file: 295 294 * "../save/data.osm" ("../" steps out of the archive) 296 295 * @param uriStr URI as string … … 308 307 } 309 308 } else if (inZipPath != null) { 310 ZipEntry entry = zipFile.getEntry(inZipPath); 311 if (entry != null) { 312 return zipFile.getInputStream(entry); 313 } 309 return Files.newInputStream(zipFile.getPath(inZipPath)); 314 310 } 315 311 throw new IOException(tr("Unable to locate file ''{0}''.", uriStr)); … … 345 341 // relative to session file - "../" step out of the archive 346 342 String relPath = uri.getPath().substring(3); 347 return new File(sessionFileURI.resolve(relPath));343 return sessionFile.resolveSibling(relPath).toFile(); 348 344 } else { 349 345 // file inside zip archive … … 352 348 } 353 349 } else 354 return new File(sessionFileURI.resolve(uri));350 return sessionFile.resolveSibling(uriStr).toFile(); 355 351 } 356 352 } else … … 704 700 public void loadSession(File sessionFile, boolean zip, ProgressMonitor progressMonitor) throws IllegalDataException, IOException { 705 701 try (InputStream josIS = createInputStream(sessionFile, zip)) { 706 loadSession(josIS, sessionFile.to URI(), zip, progressMonitor != null ? progressMonitor : NullProgressMonitor.INSTANCE);702 loadSession(josIS, sessionFile.toPath(), zip, progressMonitor != null ? progressMonitor : NullProgressMonitor.INSTANCE); 707 703 } 708 704 } … … 711 707 if (zip) { 712 708 try { 713 zipFile = new ZipFile(sessionFile, StandardCharsets.UTF_8); 714 return getZipInputStream(zipFile); 715 } catch (ZipException ex) { 716 throw new IOException(ex); 709 // read zip using ZipFileSystem/ZipFileSystemProvider 710 zipFile = FileSystems.newFileSystem(new URI("jar:" + sessionFile.toURI()), Collections.emptyMap()); 711 try (Stream<Path> content = Files.list(zipFile.getPath("/"))) { 712 final Path jos = content 713 .filter(path -> Utils.hasExtension(path.getFileName().toString(), "jos")) 714 .findFirst() 715 .orElseThrow(() -> new IllegalDataException(tr("expected .jos file inside .joz archive"))); 716 return Files.newInputStream(jos); 717 } 718 } catch (URISyntaxException e) { 719 throw new IOException(e); 717 720 } 718 721 } else { … … 721 724 } 722 725 723 private static InputStream getZipInputStream(ZipFile zipFile) throws IOException, IllegalDataException { 724 ZipEntry josEntry = null; 725 Enumeration<? extends ZipEntry> entries = zipFile.entries(); 726 while (entries.hasMoreElements()) { 727 ZipEntry entry = entries.nextElement(); 728 if (Utils.hasExtension(entry.getName(), "jos")) { 729 josEntry = entry; 730 break; 731 } 732 } 733 if (josEntry == null) { 734 error(tr("expected .jos file inside .joz archive")); 735 } 736 return zipFile.getInputStream(josEntry); 737 } 738 739 private void loadSession(InputStream josIS, URI sessionFileURI, boolean zip, ProgressMonitor progressMonitor) 726 private void loadSession(InputStream josIS, Path sessionFile, boolean zip, ProgressMonitor progressMonitor) 740 727 throws IOException, IllegalDataException { 741 728 742 this.sessionFile URI = sessionFileURI;729 this.sessionFile = sessionFile; 743 730 this.zip = zip; 744 731
Note:
See TracChangeset
for help on using the changeset viewer.