Changeset 14741 in josm for trunk/src/org/openstreetmap/josm/io/session/SessionReader.java
- Timestamp:
- 2019-01-27T18:58:32+01:00 (7 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/io/session/SessionReader.java
r14724 r14741 13 13 import java.net.URI; 14 14 import java.net.URISyntaxException; 15 import java.nio.file.FileSystem; 16 import java.nio.file.FileSystems; 15 import java.nio.charset.StandardCharsets; 17 16 import java.nio.file.Files; 18 import java.nio.file.Path;19 17 import java.util.ArrayList; 20 18 import java.util.Collection; 21 19 import java.util.Collections; 20 import java.util.Enumeration; 22 21 import java.util.HashMap; 23 22 import java.util.List; … … 25 24 import java.util.Map.Entry; 26 25 import java.util.TreeMap; 27 import java.util.stream.Stream; 26 import java.util.zip.ZipEntry; 27 import java.util.zip.ZipException; 28 import java.util.zip.ZipFile; 28 29 29 30 import javax.swing.JOptionPane; … … 150 151 private static final Map<String, Class<? extends SessionLayerImporter>> sessionLayerImporters = new HashMap<>(); 151 152 152 private PathsessionFile;153 private URI sessionFileURI; 153 154 private boolean zip; // true, if session file is a .joz file; false if it is a .jos file 154 private FileSystemzipFile;155 private ZipFile zipFile; 155 156 private List<Layer> layers = new ArrayList<>(); 156 157 private int active = -1; … … 307 308 } 308 309 } else if (inZipPath != null) { 309 return Files.newInputStream(zipFile.getPath(inZipPath)); 310 ZipEntry entry = zipFile.getEntry(inZipPath); 311 if (entry != null) { 312 return zipFile.getInputStream(entry); 313 } 310 314 } 311 315 throw new IOException(tr("Unable to locate file ''{0}''.", uriStr)); … … 341 345 // relative to session file - "../" step out of the archive 342 346 String relPath = uri.getPath().substring(3); 343 return sessionFile.resolve Sibling(relPath).toFile();347 return new File(sessionFileURI.resolve(relPath)); 344 348 } else { 345 349 // file inside zip archive … … 348 352 } 349 353 } else 350 return sessionFile.resolve Sibling(uriStr).toFile();354 return new File(sessionFileURI.resolve(uri)); 351 355 } 352 356 } else … … 700 704 public void loadSession(File sessionFile, boolean zip, ProgressMonitor progressMonitor) throws IllegalDataException, IOException { 701 705 try (InputStream josIS = createInputStream(sessionFile, zip)) { 702 loadSession(josIS, sessionFile.to Path(), zip, progressMonitor != null ? progressMonitor : NullProgressMonitor.INSTANCE);706 loadSession(josIS, sessionFile.toURI(), zip, progressMonitor != null ? progressMonitor : NullProgressMonitor.INSTANCE); 703 707 } 704 708 } … … 707 711 if (zip) { 708 712 try { 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); 713 zipFile = new ZipFile(sessionFile, StandardCharsets.UTF_8); 714 return getZipInputStream(zipFile); 715 } catch (ZipException ex) { 716 throw new IOException(ex); 720 717 } 721 718 } else { … … 724 721 } 725 722 726 private void loadSession(InputStream josIS, Path sessionFile, boolean zip, ProgressMonitor progressMonitor) 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) 727 740 throws IOException, IllegalDataException { 728 741 729 this.sessionFile = sessionFile; 742 this.sessionFileURI = sessionFileURI; 730 743 this.zip = zip; 731 744
Note:
See TracChangeset
for help on using the changeset viewer.
