PageRenderTime 65ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/src/share/demo/nio/zipfs/src/com/sun/nio/zipfs/ZipFileSystem.java

https://bitbucket.org/chegar/jigsaw_jigsaw_jdk
Java | 2358 lines | 1990 code | 172 blank | 196 comment | 502 complexity | 94cbe586c572d4446be199730c79f6c8 MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause-No-Nuclear-License-2014, LGPL-3.0
  1. /*
  2. * Copyright (c) 2009, 2011, Oracle and/or its affiliates. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. *
  8. * - Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. *
  11. * - Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. *
  15. * - Neither the name of Oracle nor the names of its
  16. * contributors may be used to endorse or promote products derived
  17. * from this software without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  20. * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  21. * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  22. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  23. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  24. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  25. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  26. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  27. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  28. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  29. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. */
  31. /*
  32. * This source code is provided to illustrate the usage of a given feature
  33. * or technique and has been deliberately simplified. Additional steps
  34. * required for a production-quality application, such as security checks,
  35. * input validation and proper error handling, might not be present in
  36. * this sample code.
  37. */
  38. package com.sun.nio.zipfs;
  39. import java.io.BufferedOutputStream;
  40. import java.io.ByteArrayInputStream;
  41. import java.io.ByteArrayOutputStream;
  42. import java.io.EOFException;
  43. import java.io.File;
  44. import java.io.IOException;
  45. import java.io.InputStream;
  46. import java.io.OutputStream;
  47. import java.nio.ByteBuffer;
  48. import java.nio.MappedByteBuffer;
  49. import java.nio.channels.*;
  50. import java.nio.file.*;
  51. import java.nio.file.attribute.*;
  52. import java.nio.file.spi.*;
  53. import java.util.*;
  54. import java.util.concurrent.locks.ReadWriteLock;
  55. import java.util.concurrent.locks.ReentrantReadWriteLock;
  56. import java.util.regex.Pattern;
  57. import java.util.zip.CRC32;
  58. import java.util.zip.Inflater;
  59. import java.util.zip.Deflater;
  60. import java.util.zip.InflaterInputStream;
  61. import java.util.zip.DeflaterOutputStream;
  62. import java.util.zip.ZipException;
  63. import java.util.zip.ZipError;
  64. import static java.lang.Boolean.*;
  65. import static com.sun.nio.zipfs.ZipConstants.*;
  66. import static com.sun.nio.zipfs.ZipUtils.*;
  67. import static java.nio.file.StandardOpenOption.*;
  68. import static java.nio.file.StandardCopyOption.*;
  69. /**
  70. * A FileSystem built on a zip file
  71. *
  72. * @author Xueming Shen
  73. */
  74. public class ZipFileSystem extends FileSystem {
  75. private final ZipFileSystemProvider provider;
  76. private final ZipPath defaultdir;
  77. private boolean readOnly = false;
  78. private final Path zfpath;
  79. private final ZipCoder zc;
  80. // configurable by env map
  81. private final String defaultDir; // default dir for the file system
  82. private final String nameEncoding; // default encoding for name/comment
  83. private final boolean useTempFile; // use a temp file for newOS, default
  84. // is to use BAOS for better performance
  85. private final boolean createNew; // create a new zip if not exists
  86. private static final boolean isWindows =
  87. System.getProperty("os.name").startsWith("Windows");
  88. ZipFileSystem(ZipFileSystemProvider provider,
  89. Path zfpath,
  90. Map<String, ?> env)
  91. throws IOException
  92. {
  93. // configurable env setup
  94. this.createNew = "true".equals(env.get("create"));
  95. this.nameEncoding = env.containsKey("encoding") ?
  96. (String)env.get("encoding") : "UTF-8";
  97. this.useTempFile = TRUE.equals(env.get("useTempFile"));
  98. this.defaultDir = env.containsKey("default.dir") ?
  99. (String)env.get("default.dir") : "/";
  100. if (this.defaultDir.charAt(0) != '/')
  101. throw new IllegalArgumentException("default dir should be absolute");
  102. this.provider = provider;
  103. this.zfpath = zfpath;
  104. if (Files.notExists(zfpath)) {
  105. if (createNew) {
  106. try (OutputStream os = Files.newOutputStream(zfpath, CREATE_NEW, WRITE)) {
  107. new END().write(os, 0);
  108. }
  109. } else {
  110. throw new FileSystemNotFoundException(zfpath.toString());
  111. }
  112. }
  113. // sm and existence check
  114. zfpath.getFileSystem().provider().checkAccess(zfpath, AccessMode.READ);
  115. if (!Files.isWritable(zfpath))
  116. this.readOnly = true;
  117. this.zc = ZipCoder.get(nameEncoding);
  118. this.defaultdir = new ZipPath(this, getBytes(defaultDir));
  119. this.ch = Files.newByteChannel(zfpath, READ);
  120. this.cen = initCEN();
  121. }
  122. @Override
  123. public FileSystemProvider provider() {
  124. return provider;
  125. }
  126. @Override
  127. public String getSeparator() {
  128. return "/";
  129. }
  130. @Override
  131. public boolean isOpen() {
  132. return isOpen;
  133. }
  134. @Override
  135. public boolean isReadOnly() {
  136. return readOnly;
  137. }
  138. private void checkWritable() throws IOException {
  139. if (readOnly)
  140. throw new ReadOnlyFileSystemException();
  141. }
  142. @Override
  143. public Iterable<Path> getRootDirectories() {
  144. ArrayList<Path> pathArr = new ArrayList<>();
  145. pathArr.add(new ZipPath(this, new byte[]{'/'}));
  146. return pathArr;
  147. }
  148. ZipPath getDefaultDir() { // package private
  149. return defaultdir;
  150. }
  151. @Override
  152. public ZipPath getPath(String first, String... more) {
  153. String path;
  154. if (more.length == 0) {
  155. path = first;
  156. } else {
  157. StringBuilder sb = new StringBuilder();
  158. sb.append(first);
  159. for (String segment: more) {
  160. if (segment.length() > 0) {
  161. if (sb.length() > 0)
  162. sb.append('/');
  163. sb.append(segment);
  164. }
  165. }
  166. path = sb.toString();
  167. }
  168. return new ZipPath(this, getBytes(path));
  169. }
  170. @Override
  171. public UserPrincipalLookupService getUserPrincipalLookupService() {
  172. throw new UnsupportedOperationException();
  173. }
  174. @Override
  175. public WatchService newWatchService() {
  176. throw new UnsupportedOperationException();
  177. }
  178. FileStore getFileStore(ZipPath path) {
  179. return new ZipFileStore(path);
  180. }
  181. @Override
  182. public Iterable<FileStore> getFileStores() {
  183. ArrayList<FileStore> list = new ArrayList<>(1);
  184. list.add(new ZipFileStore(new ZipPath(this, new byte[]{'/'})));
  185. return list;
  186. }
  187. private static final Set<String> supportedFileAttributeViews =
  188. Collections.unmodifiableSet(
  189. new HashSet<String>(Arrays.asList("basic", "zip")));
  190. @Override
  191. public Set<String> supportedFileAttributeViews() {
  192. return supportedFileAttributeViews;
  193. }
  194. @Override
  195. public String toString() {
  196. return zfpath.toString();
  197. }
  198. Path getZipFile() {
  199. return zfpath;
  200. }
  201. private static final String GLOB_SYNTAX = "glob";
  202. private static final String REGEX_SYNTAX = "regex";
  203. @Override
  204. public PathMatcher getPathMatcher(String syntaxAndInput) {
  205. int pos = syntaxAndInput.indexOf(':');
  206. if (pos <= 0 || pos == syntaxAndInput.length()) {
  207. throw new IllegalArgumentException();
  208. }
  209. String syntax = syntaxAndInput.substring(0, pos);
  210. String input = syntaxAndInput.substring(pos + 1);
  211. String expr;
  212. if (syntax.equals(GLOB_SYNTAX)) {
  213. expr = toRegexPattern(input);
  214. } else {
  215. if (syntax.equals(REGEX_SYNTAX)) {
  216. expr = input;
  217. } else {
  218. throw new UnsupportedOperationException("Syntax '" + syntax +
  219. "' not recognized");
  220. }
  221. }
  222. // return matcher
  223. final Pattern pattern = Pattern.compile(expr);
  224. return new PathMatcher() {
  225. @Override
  226. public boolean matches(Path path) {
  227. return pattern.matcher(path.toString()).matches();
  228. }
  229. };
  230. }
  231. @Override
  232. public void close() throws IOException {
  233. beginWrite();
  234. try {
  235. if (!isOpen)
  236. return;
  237. isOpen = false; // set closed
  238. } finally {
  239. endWrite();
  240. }
  241. if (!streams.isEmpty()) { // unlock and close all remaining streams
  242. Set<InputStream> copy = new HashSet<>(streams);
  243. for (InputStream is: copy)
  244. is.close();
  245. }
  246. beginWrite(); // lock and sync
  247. try {
  248. sync();
  249. ch.close(); // close the ch just in case no update
  250. } finally { // and sync dose not close the ch
  251. endWrite();
  252. }
  253. synchronized (inflaters) {
  254. for (Inflater inf : inflaters)
  255. inf.end();
  256. }
  257. synchronized (deflaters) {
  258. for (Deflater def : deflaters)
  259. def.end();
  260. }
  261. IOException ioe = null;
  262. synchronized (tmppaths) {
  263. for (Path p: tmppaths) {
  264. try {
  265. Files.deleteIfExists(p);
  266. } catch (IOException x) {
  267. if (ioe == null)
  268. ioe = x;
  269. else
  270. ioe.addSuppressed(x);
  271. }
  272. }
  273. }
  274. provider.removeFileSystem(zfpath, this);
  275. if (ioe != null)
  276. throw ioe;
  277. }
  278. ZipFileAttributes getFileAttributes(byte[] path)
  279. throws IOException
  280. {
  281. Entry e;
  282. beginRead();
  283. try {
  284. ensureOpen();
  285. e = getEntry0(path);
  286. if (e == null) {
  287. IndexNode inode = getInode(path);
  288. if (inode == null)
  289. return null;
  290. e = new Entry(inode.name); // pseudo directory
  291. e.method = METHOD_STORED; // STORED for dir
  292. e.mtime = e.atime = e.ctime = -1;// -1 for all times
  293. }
  294. } finally {
  295. endRead();
  296. }
  297. return new ZipFileAttributes(e);
  298. }
  299. void setTimes(byte[] path, FileTime mtime, FileTime atime, FileTime ctime)
  300. throws IOException
  301. {
  302. checkWritable();
  303. beginWrite();
  304. try {
  305. ensureOpen();
  306. Entry e = getEntry0(path); // ensureOpen checked
  307. if (e == null)
  308. throw new NoSuchFileException(getString(path));
  309. if (e.type == Entry.CEN)
  310. e.type = Entry.COPY; // copy e
  311. if (mtime != null)
  312. e.mtime = mtime.toMillis();
  313. if (atime != null)
  314. e.atime = atime.toMillis();
  315. if (ctime != null)
  316. e.ctime = ctime.toMillis();
  317. update(e);
  318. } finally {
  319. endWrite();
  320. }
  321. }
  322. boolean exists(byte[] path)
  323. throws IOException
  324. {
  325. beginRead();
  326. try {
  327. ensureOpen();
  328. return getInode(path) != null;
  329. } finally {
  330. endRead();
  331. }
  332. }
  333. boolean isDirectory(byte[] path)
  334. throws IOException
  335. {
  336. beginRead();
  337. try {
  338. IndexNode n = getInode(path);
  339. return n != null && n.isDir();
  340. } finally {
  341. endRead();
  342. }
  343. }
  344. private ZipPath toZipPath(byte[] path) {
  345. // make it absolute
  346. byte[] p = new byte[path.length + 1];
  347. p[0] = '/';
  348. System.arraycopy(path, 0, p, 1, path.length);
  349. return new ZipPath(this, p);
  350. }
  351. // returns the list of child paths of "path"
  352. Iterator<Path> iteratorOf(byte[] path,
  353. DirectoryStream.Filter<? super Path> filter)
  354. throws IOException
  355. {
  356. beginWrite(); // iteration of inodes needs exclusive lock
  357. try {
  358. ensureOpen();
  359. IndexNode inode = getInode(path);
  360. if (inode == null)
  361. throw new NotDirectoryException(getString(path));
  362. List<Path> list = new ArrayList<>();
  363. IndexNode child = inode.child;
  364. while (child != null) {
  365. ZipPath zp = toZipPath(child.name);
  366. if (filter == null || filter.accept(zp))
  367. list.add(zp);
  368. child = child.sibling;
  369. }
  370. return list.iterator();
  371. } finally {
  372. endWrite();
  373. }
  374. }
  375. void createDirectory(byte[] dir, FileAttribute<?>... attrs)
  376. throws IOException
  377. {
  378. checkWritable();
  379. dir = toDirectoryPath(dir);
  380. beginWrite();
  381. try {
  382. ensureOpen();
  383. if (dir.length == 0 || exists(dir)) // root dir, or exiting dir
  384. throw new FileAlreadyExistsException(getString(dir));
  385. checkParents(dir);
  386. Entry e = new Entry(dir, Entry.NEW);
  387. e.method = METHOD_STORED; // STORED for dir
  388. update(e);
  389. } finally {
  390. endWrite();
  391. }
  392. }
  393. void copyFile(boolean deletesrc, byte[]src, byte[] dst, CopyOption... options)
  394. throws IOException
  395. {
  396. checkWritable();
  397. if (Arrays.equals(src, dst))
  398. return; // do nothing, src and dst are the same
  399. beginWrite();
  400. try {
  401. ensureOpen();
  402. Entry eSrc = getEntry0(src); // ensureOpen checked
  403. if (eSrc == null)
  404. throw new NoSuchFileException(getString(src));
  405. if (eSrc.isDir()) { // spec says to create dst dir
  406. createDirectory(dst);
  407. return;
  408. }
  409. boolean hasReplace = false;
  410. boolean hasCopyAttrs = false;
  411. for (CopyOption opt : options) {
  412. if (opt == REPLACE_EXISTING)
  413. hasReplace = true;
  414. else if (opt == COPY_ATTRIBUTES)
  415. hasCopyAttrs = true;
  416. }
  417. Entry eDst = getEntry0(dst);
  418. if (eDst != null) {
  419. if (!hasReplace)
  420. throw new FileAlreadyExistsException(getString(dst));
  421. } else {
  422. checkParents(dst);
  423. }
  424. Entry u = new Entry(eSrc, Entry.COPY); // copy eSrc entry
  425. u.name(dst); // change name
  426. if (eSrc.type == Entry.NEW || eSrc.type == Entry.FILECH)
  427. {
  428. u.type = eSrc.type; // make it the same type
  429. if (!deletesrc) { // if it's not "rename", just take the data
  430. if (eSrc.bytes != null)
  431. u.bytes = Arrays.copyOf(eSrc.bytes, eSrc.bytes.length);
  432. else if (eSrc.file != null) {
  433. u.file = getTempPathForEntry(null);
  434. Files.copy(eSrc.file, u.file, REPLACE_EXISTING);
  435. }
  436. }
  437. }
  438. if (!hasCopyAttrs)
  439. u.mtime = u.atime= u.ctime = System.currentTimeMillis();
  440. update(u);
  441. if (deletesrc)
  442. updateDelete(eSrc);
  443. } finally {
  444. endWrite();
  445. }
  446. }
  447. // Returns an output stream for writing the contents into the specified
  448. // entry.
  449. OutputStream newOutputStream(byte[] path, OpenOption... options)
  450. throws IOException
  451. {
  452. checkWritable();
  453. boolean hasCreateNew = false;
  454. boolean hasCreate = false;
  455. boolean hasAppend = false;
  456. for (OpenOption opt: options) {
  457. if (opt == READ)
  458. throw new IllegalArgumentException("READ not allowed");
  459. if (opt == CREATE_NEW)
  460. hasCreateNew = true;
  461. if (opt == CREATE)
  462. hasCreate = true;
  463. if (opt == APPEND)
  464. hasAppend = true;
  465. }
  466. beginRead(); // only need a readlock, the "update()" will
  467. try { // try to obtain a writelock when the os is
  468. ensureOpen(); // being closed.
  469. Entry e = getEntry0(path);
  470. if (e != null) {
  471. if (e.isDir() || hasCreateNew)
  472. throw new FileAlreadyExistsException(getString(path));
  473. if (hasAppend) {
  474. InputStream is = getInputStream(e);
  475. OutputStream os = getOutputStream(new Entry(e, Entry.NEW));
  476. copyStream(is, os);
  477. is.close();
  478. return os;
  479. }
  480. return getOutputStream(new Entry(e, Entry.NEW));
  481. } else {
  482. if (!hasCreate && !hasCreateNew)
  483. throw new NoSuchFileException(getString(path));
  484. checkParents(path);
  485. return getOutputStream(new Entry(path, Entry.NEW));
  486. }
  487. } finally {
  488. endRead();
  489. }
  490. }
  491. // Returns an input stream for reading the contents of the specified
  492. // file entry.
  493. InputStream newInputStream(byte[] path) throws IOException {
  494. beginRead();
  495. try {
  496. ensureOpen();
  497. Entry e = getEntry0(path);
  498. if (e == null)
  499. throw new NoSuchFileException(getString(path));
  500. if (e.isDir())
  501. throw new FileSystemException(getString(path), "is a directory", null);
  502. return getInputStream(e);
  503. } finally {
  504. endRead();
  505. }
  506. }
  507. private void checkOptions(Set<? extends OpenOption> options) {
  508. // check for options of null type and option is an intance of StandardOpenOption
  509. for (OpenOption option : options) {
  510. if (option == null)
  511. throw new NullPointerException();
  512. if (!(option instanceof StandardOpenOption))
  513. throw new IllegalArgumentException();
  514. }
  515. }
  516. // Returns a Writable/ReadByteChannel for now. Might consdier to use
  517. // newFileChannel() instead, which dump the entry data into a regular
  518. // file on the default file system and create a FileChannel on top of
  519. // it.
  520. SeekableByteChannel newByteChannel(byte[] path,
  521. Set<? extends OpenOption> options,
  522. FileAttribute<?>... attrs)
  523. throws IOException
  524. {
  525. checkOptions(options);
  526. if (options.contains(StandardOpenOption.WRITE) ||
  527. options.contains(StandardOpenOption.APPEND)) {
  528. checkWritable();
  529. beginRead();
  530. try {
  531. final WritableByteChannel wbc = Channels.newChannel(
  532. newOutputStream(path, options.toArray(new OpenOption[0])));
  533. long leftover = 0;
  534. if (options.contains(StandardOpenOption.APPEND)) {
  535. Entry e = getEntry0(path);
  536. if (e != null && e.size >= 0)
  537. leftover = e.size;
  538. }
  539. final long offset = leftover;
  540. return new SeekableByteChannel() {
  541. long written = offset;
  542. public boolean isOpen() {
  543. return wbc.isOpen();
  544. }
  545. public long position() throws IOException {
  546. return written;
  547. }
  548. public SeekableByteChannel position(long pos)
  549. throws IOException
  550. {
  551. throw new UnsupportedOperationException();
  552. }
  553. public int read(ByteBuffer dst) throws IOException {
  554. throw new UnsupportedOperationException();
  555. }
  556. public SeekableByteChannel truncate(long size)
  557. throws IOException
  558. {
  559. throw new UnsupportedOperationException();
  560. }
  561. public int write(ByteBuffer src) throws IOException {
  562. int n = wbc.write(src);
  563. written += n;
  564. return n;
  565. }
  566. public long size() throws IOException {
  567. return written;
  568. }
  569. public void close() throws IOException {
  570. wbc.close();
  571. }
  572. };
  573. } finally {
  574. endRead();
  575. }
  576. } else {
  577. beginRead();
  578. try {
  579. ensureOpen();
  580. Entry e = getEntry0(path);
  581. if (e == null || e.isDir())
  582. throw new NoSuchFileException(getString(path));
  583. final ReadableByteChannel rbc =
  584. Channels.newChannel(getInputStream(e));
  585. final long size = e.size;
  586. return new SeekableByteChannel() {
  587. long read = 0;
  588. public boolean isOpen() {
  589. return rbc.isOpen();
  590. }
  591. public long position() throws IOException {
  592. return read;
  593. }
  594. public SeekableByteChannel position(long pos)
  595. throws IOException
  596. {
  597. throw new UnsupportedOperationException();
  598. }
  599. public int read(ByteBuffer dst) throws IOException {
  600. int n = rbc.read(dst);
  601. if (n > 0) {
  602. read += n;
  603. }
  604. return n;
  605. }
  606. public SeekableByteChannel truncate(long size)
  607. throws IOException
  608. {
  609. throw new NonWritableChannelException();
  610. }
  611. public int write (ByteBuffer src) throws IOException {
  612. throw new NonWritableChannelException();
  613. }
  614. public long size() throws IOException {
  615. return size;
  616. }
  617. public void close() throws IOException {
  618. rbc.close();
  619. }
  620. };
  621. } finally {
  622. endRead();
  623. }
  624. }
  625. }
  626. // Returns a FileChannel of the specified entry.
  627. //
  628. // This implementation creates a temporary file on the default file system,
  629. // copy the entry data into it if the entry exists, and then create a
  630. // FileChannel on top of it.
  631. FileChannel newFileChannel(byte[] path,
  632. Set<? extends OpenOption> options,
  633. FileAttribute<?>... attrs)
  634. throws IOException
  635. {
  636. checkOptions(options);
  637. final boolean forWrite = (options.contains(StandardOpenOption.WRITE) ||
  638. options.contains(StandardOpenOption.APPEND));
  639. beginRead();
  640. try {
  641. ensureOpen();
  642. Entry e = getEntry0(path);
  643. if (forWrite) {
  644. checkWritable();
  645. if (e == null) {
  646. if (!options.contains(StandardOpenOption.CREATE_NEW))
  647. throw new NoSuchFileException(getString(path));
  648. } else {
  649. if (options.contains(StandardOpenOption.CREATE_NEW))
  650. throw new FileAlreadyExistsException(getString(path));
  651. if (e.isDir())
  652. throw new FileAlreadyExistsException("directory <"
  653. + getString(path) + "> exists");
  654. }
  655. options.remove(StandardOpenOption.CREATE_NEW); // for tmpfile
  656. } else if (e == null || e.isDir()) {
  657. throw new NoSuchFileException(getString(path));
  658. }
  659. final boolean isFCH = (e != null && e.type == Entry.FILECH);
  660. final Path tmpfile = isFCH ? e.file : getTempPathForEntry(path);
  661. final FileChannel fch = tmpfile.getFileSystem()
  662. .provider()
  663. .newFileChannel(tmpfile, options, attrs);
  664. final Entry u = isFCH ? e : new Entry(path, tmpfile, Entry.FILECH);
  665. if (forWrite) {
  666. u.flag = FLAG_DATADESCR;
  667. u.method = METHOD_DEFLATED;
  668. }
  669. // is there a better way to hook into the FileChannel's close method?
  670. return new FileChannel() {
  671. public int write(ByteBuffer src) throws IOException {
  672. return fch.write(src);
  673. }
  674. public long write(ByteBuffer[] srcs, int offset, int length)
  675. throws IOException
  676. {
  677. return fch.write(srcs, offset, length);
  678. }
  679. public long position() throws IOException {
  680. return fch.position();
  681. }
  682. public FileChannel position(long newPosition)
  683. throws IOException
  684. {
  685. fch.position(newPosition);
  686. return this;
  687. }
  688. public long size() throws IOException {
  689. return fch.size();
  690. }
  691. public FileChannel truncate(long size)
  692. throws IOException
  693. {
  694. fch.truncate(size);
  695. return this;
  696. }
  697. public void force(boolean metaData)
  698. throws IOException
  699. {
  700. fch.force(metaData);
  701. }
  702. public long transferTo(long position, long count,
  703. WritableByteChannel target)
  704. throws IOException
  705. {
  706. return fch.transferTo(position, count, target);
  707. }
  708. public long transferFrom(ReadableByteChannel src,
  709. long position, long count)
  710. throws IOException
  711. {
  712. return fch.transferFrom(src, position, count);
  713. }
  714. public int read(ByteBuffer dst) throws IOException {
  715. return fch.read(dst);
  716. }
  717. public int read(ByteBuffer dst, long position)
  718. throws IOException
  719. {
  720. return fch.read(dst, position);
  721. }
  722. public long read(ByteBuffer[] dsts, int offset, int length)
  723. throws IOException
  724. {
  725. return fch.read(dsts, offset, length);
  726. }
  727. public int write(ByteBuffer src, long position)
  728. throws IOException
  729. {
  730. return fch.write(src, position);
  731. }
  732. public MappedByteBuffer map(MapMode mode,
  733. long position, long size)
  734. throws IOException
  735. {
  736. throw new UnsupportedOperationException();
  737. }
  738. public FileLock lock(long position, long size, boolean shared)
  739. throws IOException
  740. {
  741. return fch.lock(position, size, shared);
  742. }
  743. public FileLock tryLock(long position, long size, boolean shared)
  744. throws IOException
  745. {
  746. return fch.tryLock(position, size, shared);
  747. }
  748. protected void implCloseChannel() throws IOException {
  749. fch.close();
  750. if (forWrite) {
  751. u.mtime = System.currentTimeMillis();
  752. u.size = Files.size(u.file);
  753. update(u);
  754. } else {
  755. if (!isFCH) // if this is a new fch for reading
  756. removeTempPathForEntry(tmpfile);
  757. }
  758. }
  759. };
  760. } finally {
  761. endRead();
  762. }
  763. }
  764. // the outstanding input streams that need to be closed
  765. private Set<InputStream> streams =
  766. Collections.synchronizedSet(new HashSet<InputStream>());
  767. // the ex-channel and ex-path that need to close when their outstanding
  768. // input streams are all closed by the obtainers.
  769. private Set<ExChannelCloser> exChClosers = new HashSet<>();
  770. private Set<Path> tmppaths = Collections.synchronizedSet(new HashSet<Path>());
  771. private Path getTempPathForEntry(byte[] path) throws IOException {
  772. Path tmpPath = createTempFileInSameDirectoryAs(zfpath);
  773. if (path != null) {
  774. Entry e = getEntry0(path);
  775. if (e != null) {
  776. try (InputStream is = newInputStream(path)) {
  777. Files.copy(is, tmpPath, REPLACE_EXISTING);
  778. }
  779. }
  780. }
  781. return tmpPath;
  782. }
  783. private void removeTempPathForEntry(Path path) throws IOException {
  784. Files.delete(path);
  785. tmppaths.remove(path);
  786. }
  787. // check if all parents really exit. ZIP spec does not require
  788. // the existence of any "parent directory".
  789. private void checkParents(byte[] path) throws IOException {
  790. beginRead();
  791. try {
  792. while ((path = getParent(path)) != null && path.length != 0) {
  793. if (!inodes.containsKey(IndexNode.keyOf(path))) {
  794. throw new NoSuchFileException(getString(path));
  795. }
  796. }
  797. } finally {
  798. endRead();
  799. }
  800. }
  801. private static byte[] ROOTPATH = new byte[0];
  802. private static byte[] getParent(byte[] path) {
  803. int off = path.length - 1;
  804. if (off > 0 && path[off] == '/') // isDirectory
  805. off--;
  806. while (off > 0 && path[off] != '/') { off--; }
  807. if (off <= 0)
  808. return ROOTPATH;
  809. return Arrays.copyOf(path, off + 1);
  810. }
  811. private final void beginWrite() {
  812. rwlock.writeLock().lock();
  813. }
  814. private final void endWrite() {
  815. rwlock.writeLock().unlock();
  816. }
  817. private final void beginRead() {
  818. rwlock.readLock().lock();
  819. }
  820. private final void endRead() {
  821. rwlock.readLock().unlock();
  822. }
  823. ///////////////////////////////////////////////////////////////////
  824. private volatile boolean isOpen = true;
  825. private final SeekableByteChannel ch; // channel to the zipfile
  826. final byte[] cen; // CEN & ENDHDR
  827. private END end;
  828. private long locpos; // position of first LOC header (usually 0)
  829. private final ReadWriteLock rwlock = new ReentrantReadWriteLock();
  830. // name -> pos (in cen), IndexNode itself can be used as a "key"
  831. private LinkedHashMap<IndexNode, IndexNode> inodes;
  832. final byte[] getBytes(String name) {
  833. return zc.getBytes(name);
  834. }
  835. final String getString(byte[] name) {
  836. return zc.toString(name);
  837. }
  838. protected void finalize() throws IOException {
  839. close();
  840. }
  841. private long getDataPos(Entry e) throws IOException {
  842. if (e.locoff == -1) {
  843. Entry e2 = getEntry0(e.name);
  844. if (e2 == null)
  845. throw new ZipException("invalid loc for entry <" + e.name + ">");
  846. e.locoff = e2.locoff;
  847. }
  848. byte[] buf = new byte[LOCHDR];
  849. if (readFullyAt(buf, 0, buf.length, e.locoff) != buf.length)
  850. throw new ZipException("invalid loc for entry <" + e.name + ">");
  851. return locpos + e.locoff + LOCHDR + LOCNAM(buf) + LOCEXT(buf);
  852. }
  853. // Reads len bytes of data from the specified offset into buf.
  854. // Returns the total number of bytes read.
  855. // Each/every byte read from here (except the cen, which is mapped).
  856. final long readFullyAt(byte[] buf, int off, long len, long pos)
  857. throws IOException
  858. {
  859. ByteBuffer bb = ByteBuffer.wrap(buf);
  860. bb.position(off);
  861. bb.limit((int)(off + len));
  862. return readFullyAt(bb, pos);
  863. }
  864. private final long readFullyAt(ByteBuffer bb, long pos)
  865. throws IOException
  866. {
  867. synchronized(ch) {
  868. return ch.position(pos).read(bb);
  869. }
  870. }
  871. // Searches for end of central directory (END) header. The contents of
  872. // the END header will be read and placed in endbuf. Returns the file
  873. // position of the END header, otherwise returns -1 if the END header
  874. // was not found or an error occurred.
  875. private END findEND() throws IOException
  876. {
  877. byte[] buf = new byte[READBLOCKSZ];
  878. long ziplen = ch.size();
  879. long minHDR = (ziplen - END_MAXLEN) > 0 ? ziplen - END_MAXLEN : 0;
  880. long minPos = minHDR - (buf.length - ENDHDR);
  881. for (long pos = ziplen - buf.length; pos >= minPos; pos -= (buf.length - ENDHDR))
  882. {
  883. int off = 0;
  884. if (pos < 0) {
  885. // Pretend there are some NUL bytes before start of file
  886. off = (int)-pos;
  887. Arrays.fill(buf, 0, off, (byte)0);
  888. }
  889. int len = buf.length - off;
  890. if (readFullyAt(buf, off, len, pos + off) != len)
  891. zerror("zip END header not found");
  892. // Now scan the block backwards for END header signature
  893. for (int i = buf.length - ENDHDR; i >= 0; i--) {
  894. if (buf[i+0] == (byte)'P' &&
  895. buf[i+1] == (byte)'K' &&
  896. buf[i+2] == (byte)'\005' &&
  897. buf[i+3] == (byte)'\006' &&
  898. (pos + i + ENDHDR + ENDCOM(buf, i) == ziplen)) {
  899. // Found END header
  900. buf = Arrays.copyOfRange(buf, i, i + ENDHDR);
  901. END end = new END();
  902. end.endsub = ENDSUB(buf);
  903. end.centot = ENDTOT(buf);
  904. end.cenlen = ENDSIZ(buf);
  905. end.cenoff = ENDOFF(buf);
  906. end.comlen = ENDCOM(buf);
  907. end.endpos = pos + i;
  908. if (end.cenlen == ZIP64_MINVAL ||
  909. end.cenoff == ZIP64_MINVAL ||
  910. end.centot == ZIP64_MINVAL32)
  911. {
  912. // need to find the zip64 end;
  913. byte[] loc64 = new byte[ZIP64_LOCHDR];
  914. if (readFullyAt(loc64, 0, loc64.length, end.endpos - ZIP64_LOCHDR)
  915. != loc64.length) {
  916. return end;
  917. }
  918. long end64pos = ZIP64_LOCOFF(loc64);
  919. byte[] end64buf = new byte[ZIP64_ENDHDR];
  920. if (readFullyAt(end64buf, 0, end64buf.length, end64pos)
  921. != end64buf.length) {
  922. return end;
  923. }
  924. // end64 found, re-calcualte everything.
  925. end.cenlen = ZIP64_ENDSIZ(end64buf);
  926. end.cenoff = ZIP64_ENDOFF(end64buf);
  927. end.centot = (int)ZIP64_ENDTOT(end64buf); // assume total < 2g
  928. end.endpos = end64pos;
  929. }
  930. return end;
  931. }
  932. }
  933. }
  934. zerror("zip END header not found");
  935. return null; //make compiler happy
  936. }
  937. // Reads zip file central directory. Returns the file position of first
  938. // CEN header, otherwise returns -1 if an error occured. If zip->msg != NULL
  939. // then the error was a zip format error and zip->msg has the error text.
  940. // Always pass in -1 for knownTotal; it's used for a recursive call.
  941. private byte[] initCEN() throws IOException {
  942. end = findEND();
  943. if (end.endpos == 0) {
  944. inodes = new LinkedHashMap<>(10);
  945. locpos = 0;
  946. buildNodeTree();
  947. return null; // only END header present
  948. }
  949. if (end.cenlen > end.endpos)
  950. zerror("invalid END header (bad central directory size)");
  951. long cenpos = end.endpos - end.cenlen; // position of CEN table
  952. // Get position of first local file (LOC) header, taking into
  953. // account that there may be a stub prefixed to the zip file.
  954. locpos = cenpos - end.cenoff;
  955. if (locpos < 0)
  956. zerror("invalid END header (bad central directory offset)");
  957. // read in the CEN and END
  958. byte[] cen = new byte[(int)(end.cenlen + ENDHDR)];
  959. if (readFullyAt(cen, 0, cen.length, cenpos) != end.cenlen + ENDHDR) {
  960. zerror("read CEN tables failed");
  961. }
  962. // Iterate through the entries in the central directory
  963. inodes = new LinkedHashMap<>(end.centot + 1);
  964. int pos = 0;
  965. int limit = cen.length - ENDHDR;
  966. while (pos < limit) {
  967. if (CENSIG(cen, pos) != CENSIG)
  968. zerror("invalid CEN header (bad signature)");
  969. int method = CENHOW(cen, pos);
  970. int nlen = CENNAM(cen, pos);
  971. int elen = CENEXT(cen, pos);
  972. int clen = CENCOM(cen, pos);
  973. if ((CENFLG(cen, pos) & 1) != 0)
  974. zerror("invalid CEN header (encrypted entry)");
  975. if (method != METHOD_STORED && method != METHOD_DEFLATED)
  976. zerror("invalid CEN header (unsupported compression method: " + method + ")");
  977. if (pos + CENHDR + nlen > limit)
  978. zerror("invalid CEN header (bad header size)");
  979. byte[] name = Arrays.copyOfRange(cen, pos + CENHDR, pos + CENHDR + nlen);
  980. IndexNode inode = new IndexNode(name, pos);
  981. inodes.put(inode, inode);
  982. // skip ext and comment
  983. pos += (CENHDR + nlen + elen + clen);
  984. }
  985. if (pos + ENDHDR != cen.length) {
  986. zerror("invalid CEN header (bad header size)");
  987. }
  988. buildNodeTree();
  989. return cen;
  990. }
  991. private void ensureOpen() throws IOException {
  992. if (!isOpen)
  993. throw new ClosedFileSystemException();
  994. }
  995. // Creates a new empty temporary file in the same directory as the
  996. // specified file. A variant of File.createTempFile.
  997. private Path createTempFileInSameDirectoryAs(Path path)
  998. throws IOException
  999. {
  1000. Path parent = path.toAbsolutePath().getParent();
  1001. String dir = (parent == null)? "." : parent.toString();
  1002. Path tmpPath = File.createTempFile("zipfstmp", null, new File(dir)).toPath();
  1003. tmppaths.add(tmpPath);
  1004. return tmpPath;
  1005. }
  1006. ////////////////////update & sync //////////////////////////////////////
  1007. private boolean hasUpdate = false;
  1008. // shared key. consumer guarantees the "writeLock" before use it.
  1009. private final IndexNode LOOKUPKEY = IndexNode.keyOf(null);
  1010. private void updateDelete(IndexNode inode) {
  1011. beginWrite();
  1012. try {
  1013. removeFromTree(inode);
  1014. inodes.remove(inode);
  1015. hasUpdate = true;
  1016. } finally {
  1017. endWrite();
  1018. }
  1019. }
  1020. private void update(Entry e) {
  1021. beginWrite();
  1022. try {
  1023. IndexNode old = inodes.put(e, e);
  1024. if (old != null) {
  1025. removeFromTree(old);
  1026. }
  1027. if (e.type == Entry.NEW || e.type == Entry.FILECH) {
  1028. IndexNode parent = inodes.get(LOOKUPKEY.as(getParent(e.name)));
  1029. e.sibling = parent.child;
  1030. parent.child = e;
  1031. }
  1032. hasUpdate = true;
  1033. } finally {
  1034. endWrite();
  1035. }
  1036. }
  1037. // copy over the whole LOC entry (header if necessary, data and ext) from
  1038. // old zip to the new one.
  1039. private long copyLOCEntry(Entry e, boolean updateHeader,
  1040. OutputStream os,
  1041. long written, byte[] buf)
  1042. throws IOException
  1043. {
  1044. long locoff = e.locoff; // where to read
  1045. e.locoff = written; // update the e.locoff with new value
  1046. // calculate the size need to write out
  1047. long size = 0;
  1048. // if there is A ext
  1049. if ((e.flag & FLAG_DATADESCR) != 0) {
  1050. if (e.size >= ZIP64_MINVAL || e.csize >= ZIP64_MINVAL)
  1051. size = 24;
  1052. else
  1053. size = 16;
  1054. }
  1055. // read loc, use the original loc.elen/nlen
  1056. if (readFullyAt(buf, 0, LOCHDR , locoff) != LOCHDR)
  1057. throw new ZipException("loc: reading failed");
  1058. if (updateHeader) {
  1059. locoff += LOCHDR + LOCNAM(buf) + LOCEXT(buf); // skip header
  1060. size += e.csize;
  1061. written = e.writeLOC(os) + size;
  1062. } else {
  1063. os.write(buf, 0, LOCHDR); // write out the loc header
  1064. locoff += LOCHDR;
  1065. // use e.csize, LOCSIZ(buf) is zero if FLAG_DATADESCR is on
  1066. // size += LOCNAM(buf) + LOCEXT(buf) + LOCSIZ(buf);
  1067. size += LOCNAM(buf) + LOCEXT(buf) + e.csize;
  1068. written = LOCHDR + size;
  1069. }
  1070. int n;
  1071. while (size > 0 &&
  1072. (n = (int)readFullyAt(buf, 0, buf.length, locoff)) != -1)
  1073. {
  1074. if (size < n)
  1075. n = (int)size;
  1076. os.write(buf, 0, n);
  1077. size -= n;
  1078. locoff += n;
  1079. }
  1080. return written;
  1081. }
  1082. // sync the zip file system, if there is any udpate
  1083. private void sync() throws IOException {
  1084. //System.out.printf("->sync(%s) starting....!%n", toString());
  1085. // check ex-closer
  1086. if (!exChClosers.isEmpty()) {
  1087. for (ExChannelCloser ecc : exChClosers) {
  1088. if (ecc.streams.isEmpty()) {
  1089. ecc.ch.close();
  1090. Files.delete(ecc.path);
  1091. exChClosers.remove(ecc);
  1092. }
  1093. }
  1094. }
  1095. if (!hasUpdate)
  1096. return;
  1097. Path tmpFile = createTempFileInSameDirectoryAs(zfpath);
  1098. try (OutputStream os = new BufferedOutputStream(Files.newOutputStream(tmpFile, WRITE)))
  1099. {
  1100. ArrayList<Entry> elist = new ArrayList<>(inodes.size());
  1101. long written = 0;
  1102. byte[] buf = new byte[8192];
  1103. Entry e = null;
  1104. // write loc
  1105. for (IndexNode inode : inodes.values()) {
  1106. if (inode instanceof Entry) { // an updated inode
  1107. e = (Entry)inode;
  1108. try {
  1109. if (e.type == Entry.COPY) {
  1110. // entry copy: the only thing changed is the "name"
  1111. // and "nlen" in LOC header, so we udpate/rewrite the
  1112. // LOC in new file and simply copy the rest (data and
  1113. // ext) without enflating/deflating from the old zip
  1114. // file LOC entry.
  1115. written += copyLOCEntry(e, true, os, written, buf);
  1116. } else { // NEW, FILECH or CEN
  1117. e.locoff = written;
  1118. written += e.writeLOC(os); // write loc header
  1119. if (e.bytes != null) { // in-memory, deflated
  1120. os.write(e.bytes); // already
  1121. written += e.bytes.length;
  1122. } else if (e.file != null) { // tmp file
  1123. try (InputStream is = Files.newInputStream(e.file)) {
  1124. int n;
  1125. if (e.type == Entry.NEW) { // deflated already
  1126. while ((n = is.read(buf)) != -1) {
  1127. os.write(buf, 0, n);
  1128. written += n;
  1129. }
  1130. } else if (e.type == Entry.FILECH) {
  1131. // the data are not deflated, use ZEOS
  1132. try (OutputStream os2 = new EntryOutputStream(e, os)) {
  1133. while ((n = is.read(buf)) != -1) {
  1134. os2.write(buf, 0, n);
  1135. }
  1136. }
  1137. written += e.csize;
  1138. if ((e.flag & FLAG_DATADESCR) != 0)
  1139. written += e.writeEXT(os);
  1140. }
  1141. }
  1142. Files.delete(e.file);
  1143. tmppaths.remove(e.file);
  1144. } else {
  1145. // dir, 0-length data
  1146. }
  1147. }
  1148. elist.add(e);
  1149. } catch (IOException x) {
  1150. x.printStackTrace(); // skip any in-accurate entry
  1151. }
  1152. } else { // unchanged inode
  1153. if (inode.pos == -1) {
  1154. continue; // pseudo directory node
  1155. }
  1156. e = Entry.readCEN(this, inode.pos);
  1157. try {
  1158. written += copyLOCEntry(e, false, os, written, buf);
  1159. elist.add(e);
  1160. } catch (IOException x) {
  1161. x.printStackTrace(); // skip any wrong entry
  1162. }
  1163. }
  1164. }
  1165. // now write back the cen and end table
  1166. end.cenoff = written;
  1167. for (Entry entry : elist) {
  1168. written += entry.writeCEN(os);
  1169. }
  1170. end.centot = elist.size();
  1171. end.cenlen = written - end.cenoff;
  1172. end.write(os, written);
  1173. }
  1174. if (!streams.isEmpty()) {
  1175. //
  1176. // TBD: ExChannelCloser should not be necessary if we only
  1177. // sync when being closed, all streams should have been
  1178. // closed already. Keep the logic here for now.
  1179. //
  1180. // There are outstanding input streams open on existing "ch",
  1181. // so, don't close the "cha" and delete the "file for now, let
  1182. // the "ex-channel-closer" to handle them
  1183. ExChannelCloser ecc = new ExChannelCloser(
  1184. createTempFileInSameDirectoryAs(zfpath),
  1185. ch,
  1186. streams);
  1187. Files.move(zfpath, ecc.path, REPLACE_EXISTING);
  1188. exChClosers.add(ecc);
  1189. streams = Collections.synchronizedSet(new HashSet<InputStream>());
  1190. } else {
  1191. ch.close();
  1192. Files.delete(zfpath);
  1193. }
  1194. Files.move(tmpFile, zfpath, REPLACE_EXISTING);
  1195. hasUpdate = false; // clear
  1196. /*
  1197. if (isOpen) {
  1198. ch = zfpath.newByteChannel(READ); // re-fresh "ch" and "cen"
  1199. cen = initCEN();
  1200. }
  1201. */
  1202. //System.out.printf("->sync(%s) done!%n", toString());
  1203. }
  1204. private IndexNode getInode(byte[] path) {
  1205. if (path == null)
  1206. throw new NullPointerException("path");
  1207. IndexNode key = IndexNode.keyOf(path);
  1208. IndexNode inode = inodes.get(key);
  1209. if (inode == null &&
  1210. (path.length == 0 || path[path.length -1] != '/')) {
  1211. // if does not ends with a slash
  1212. path = Arrays.copyOf(path, path.length + 1);
  1213. path[path.length - 1] = '/';
  1214. inode = inodes.get(key.as(path));
  1215. }
  1216. return inode;
  1217. }
  1218. private Entry getEntry0(byte[] path) throws IOException {
  1219. IndexNode inode = getInode(path);
  1220. if (inode instanceof Entry)
  1221. return (Entry)inode;
  1222. if (inode == null || inode.pos == -1)
  1223. return null;
  1224. return Entry.readCEN(this, inode.pos);
  1225. }
  1226. public void deleteFile(byte[] path, boolean failIfNotExists)
  1227. throws IOException
  1228. {
  1229. checkWritable();
  1230. IndexNode inode = getInode(path);
  1231. if (inode == null) {
  1232. if (path != null && path.length == 0)
  1233. throw new ZipException("root directory </> can't not be delete");
  1234. if (failIfNotExists)
  1235. throw new NoSuchFileException(getString(path));
  1236. } else {
  1237. if (inode.isDir() && inode.child != null)
  1238. throw new DirectoryNotEmptyException(getString(path));
  1239. updateDelete(inode);
  1240. }
  1241. }
  1242. private static void copyStream(InputStream is, OutputStream os)
  1243. throws IOException
  1244. {
  1245. byte[] copyBuf = new byte[8192];
  1246. int n;
  1247. while ((n = is.read(copyBuf)) != -1) {
  1248. os.write(copyBuf, 0, n);
  1249. }
  1250. }
  1251. // Returns an out stream for either
  1252. // (1) writing the contents of a new entry, if the entry exits, or
  1253. // (2) updating/replacing the contents of the specified existing entry.
  1254. private OutputStream getOutputStream(Entry e) throws IOException {
  1255. if (e.mtime == -1)
  1256. e.mtime = System.currentTimeMillis();
  1257. if (e.method == -1)
  1258. e.method = METHOD_DEFLATED; // TBD: use default method
  1259. // store size, compressed size, and crc-32 in LOC header
  1260. e.flag = 0;
  1261. if (zc.isUTF8())
  1262. e.flag |= FLAG_EFS;
  1263. OutputStream os;
  1264. if (useTempFile) {
  1265. e.file = getTempPathForEntry(null);
  1266. os = Files.newOutputStream(e.file, WRITE);
  1267. } else {
  1268. os = new ByteArrayOutputStream((e.size > 0)? (int)e.size : 8192);
  1269. }
  1270. return new EntryOutputStream(e, os);
  1271. }
  1272. private InputStream getInputStream(Entry e)
  1273. throws IOException
  1274. {
  1275. InputStream eis = null;
  1276. if (e.type == Entry.NEW) {
  1277. if (e.bytes != null)
  1278. eis = new ByteArrayInputStream(e.bytes);
  1279. else if (e.file != null)
  1280. eis = Files.newInputStream(e.file);
  1281. else
  1282. throw new ZipException("update entry data is missing");
  1283. } else if (e.type == Entry.FILECH) {
  1284. // FILECH result is un-compressed.
  1285. eis = Files.newInputStream(e.file);
  1286. // TBD: wrap to hook close()
  1287. // streams.add(eis);
  1288. return eis;
  1289. } else { // untouced CEN or COPY
  1290. eis = new EntryInputStream(e, ch);
  1291. }
  1292. if (e.method == METHOD_DEFLATED) {
  1293. // MORE: Compute good size for inflater stream:
  1294. long bufSize = e.size + 2; // Inflater likes a bit of slack
  1295. if (bufSize > 65536)
  1296. bufSize = 8192;
  1297. final long size = e.size;
  1298. eis = new InflaterInputStream(eis, getInflater(), (int)bufSize) {
  1299. private boolean isClosed = false;
  1300. public void close() throws IOException {
  1301. if (!isClosed) {
  1302. releaseInflater(inf);
  1303. this.in.close();
  1304. isClosed = true;
  1305. streams.remove(this);
  1306. }
  1307. }
  1308. // Override fill() method to provide an extra "dummy" byte
  1309. // at the end of the input stream. This is required when
  1310. // using the "nowrap" Inflater option. (it appears the new
  1311. // zlib in 7 does not need it, but keep it for now)
  1312. protected void fill() throws IOException {
  1313. if (eof) {
  1314. throw new EOFException(
  1315. "Unexpected end of ZLIB input stream");
  1316. }
  1317. len = this.in.read(buf, 0, buf.length);
  1318. if (len == -1) {
  1319. buf[0] = 0;
  1320. len = 1;
  1321. eof = true;
  1322. }
  1323. inf.setInput(buf, 0, len);
  1324. }
  1325. private boolean eof;
  1326. public int available() throws IOException {
  1327. if (isClosed)
  1328. return 0;
  1329. long avail = size - inf.getBytesWritten();
  1330. return avail > (long) Integer.MAX_VALUE ?
  1331. Integer.MAX_VALUE : (int) avail;
  1332. }
  1333. };
  1334. } else if (e.method == METHOD_STORED) {
  1335. // TBD: wrap/ it does not seem necessary
  1336. } else {
  1337. throw new ZipException("invalid compression method");
  1338. }
  1339. streams.add(eis);
  1340. return eis;
  1341. }
  1342. // Inner class implementing the input stream used to read
  1343. // a (possibly compressed) zip file entry.
  1344. private class EntryInputStream extends InputStream {
  1345. private final SeekableByteChannel zfch; // local ref to zipfs's "ch". zipfs.ch might
  1346. // point to a new channel after sync()
  1347. private long pos; // current position within entry data
  1348. protected long rem; // number of remaining bytes within entry
  1349. protected final long size; // uncompressed size of this entry
  1350. EntryInputStream(Entry e, SeekableByteChannel zfch)
  1351. throws IOException
  1352. {
  1353. this.zfch = zfch;
  1354. rem = e.csize;
  1355. size = e.size;
  1356. pos = getDataPos(e);
  1357. }
  1358. public int read(byte b[], int off, int len) throws IOException {
  1359. ensureOpen();
  1360. if (rem == 0) {
  1361. return -1;
  1362. }
  1363. if (len <= 0) {
  1364. return 0;
  1365. }
  1366. if (len > rem) {
  1367. len = (int) rem;
  1368. }
  1369. // readFullyAt()
  1370. long n = 0;
  1371. ByteBuffer bb = ByteBuffer.wrap(b);
  1372. bb.position(off);
  1373. bb.limit(off + len);
  1374. synchronized(zfch) {
  1375. n = zfch.position(pos).read(bb);
  1376. }
  1377. if (n > 0) {
  1378. pos += n;
  1379. rem -= n;
  1380. }
  1381. if (rem == 0) {
  1382. close();
  1383. }
  1384. return (int)n;
  1385. }
  1386. public int read() throws IOException {
  1387. byte[] b = new byte[1];
  1388. if (read(b, 0, 1) == 1) {
  1389. return b[0] & 0xff;
  1390. } else {
  1391. return -1;
  1392. }
  1393. }
  1394. public long skip(long n) throws IOException {
  1395. ensureOpen();
  1396. if (n > rem)
  1397. n = rem;
  1398. pos += n;
  1399. rem -= n;
  1400. if (rem == 0) {
  1401. close();
  1402. }
  1403. return n;
  1404. }
  1405. public int available() {
  1406. return rem > Integer.MAX_VALUE ? Integer.MAX_VALUE : (int) rem;
  1407. }
  1408. public long size() {
  1409. return size;
  1410. }
  1411. public void close() {
  1412. rem = 0;
  1413. streams.remove(this);
  1414. }
  1415. }
  1416. class EntryOutputStream extends DeflaterOutputStream
  1417. {
  1418. private CRC32 crc;
  1419. private Entry e;
  1420. private long written;
  1421. EntryOutputStream(Entry e, OutputStream os)
  1422. throws IOException
  1423. {
  1424. super(os, getDeflater());
  1425. if (e == null)
  1426. throw new NullPointerException("Zip entry is null");
  1427. this.e = e;
  1428. crc = new CRC32();
  1429. }
  1430. @Override
  1431. public void write(byte b[], int off, int len) throws IOException {
  1432. if (e.type != Entry.FILECH) // only from sync
  1433. ensureOpen();
  1434. if (off < 0 || len < 0 || off > b.length - len) {
  1435. throw new IndexOutOfBoundsException();
  1436. } else if (len == 0) {
  1437. return;
  1438. }
  1439. switch (e.method) {
  1440. case METHOD_DEFLATED:
  1441. super.write(b, off, len);
  1442. break;
  1443. case METHOD_STORED:
  1444. written += len;
  1445. out.write(b, off, len);
  1446. break;
  1447. default:
  1448. throw new ZipException("invalid compression method");
  1449. }
  1450. crc.update(b, off, len);
  1451. }
  1452. @Override
  1453. public void close() throws IOException {
  1454. // TBD ensureOpen();
  1455. switch (e.method) {
  1456. case METHOD_DEFLATED:
  1457. finish();
  1458. e.size = def.getBytesRead();
  1459. e.csize = def.getBytesWritten();
  1460. e.crc = crc.getValue();
  1461. break;
  1462. case METHOD_STORED:
  1463. // we already know that both e.size and e.csize are the same
  1464. e.size = e.csize = written;
  1465. e.crc = crc.getValue();
  1466. break;
  1467. default:
  1468. throw new ZipException("invalid compression method");
  1469. }
  1470. //crc.reset();
  1471. if (out instanceof ByteArrayOutputStream)
  1472. e.bytes = ((ByteArrayOutputStream)out).toByteArray();
  1473. if (e.type == Entry.FILECH) {
  1474. releaseDeflater(def);
  1475. return;
  1476. }
  1477. super.close();
  1478. releaseDeflater(def);
  1479. update(e);
  1480. }
  1481. }
  1482. static void zerror(String msg) {
  1483. throw new ZipError(msg);
  1484. }
  1485. // Maxmum number of de/inflater we cache
  1486. private final int MAX_FLATER = 20;
  1487. // List of available Inflater objects for decompression
  1488. private final List<Inflater> inflaters = new ArrayList<>();
  1489. // Gets an inflater from the list of available inflaters or allocates
  1490. // a new one.
  1491. private Inflater getInflater() {
  1492. synchronized (inflaters) {
  1493. int size = inflaters.size();
  1494. if (size > 0) {
  1495. Inflater inf = inflaters.remove(size - 1);
  1496. return inf;
  1497. } else {
  1498. return new Inflater(true);
  1499. }
  1500. }
  1501. }
  1502. // Releases the specified inflater to the list of available inflaters.
  1503. private void releaseInflater(Inflater inf) {
  1504. synchronized (inflaters) {
  1505. if (inflaters.size() < MAX_FLATER) {
  1506. inf.reset();
  1507. inflaters.add(inf);
  1508. } else {
  1509. inf.end();
  1510. }
  1511. }
  1512. }
  1513. // List of available Deflater objects for compression
  1514. private final List<Deflater> deflaters = new ArrayList<>();
  1515. // Gets an deflater from the list of available deflaters or allocates
  1516. // a new one.
  1517. private Deflater getDeflater() {
  1518. synchronized (deflaters) {
  1519. int size = deflaters.size();
  1520. if (size > 0) {
  1521. Deflater def = deflaters.remove(size - 1);
  1522. return def;
  1523. } else {
  1524. return new Deflater(Deflater.DEFAULT_COMPRESSION, true);
  1525. }
  1526. }
  1527. }
  1528. // Releases the specified inflater to the list of available inflaters.
  1529. private void releaseDeflater(Deflater def) {
  1530. synchronized (deflaters) {
  1531. if (inflaters.size() < MAX_FLATER) {
  1532. def.reset();
  1533. deflaters.add(def);
  1534. } else {
  1535. def.end();
  1536. }
  1537. }
  1538. }
  1539. // End of central directory record
  1540. static class END {
  1541. int disknum;
  1542. int sdisknum;
  1543. int endsub; // endsub
  1544. int centot; // 4 bytes
  1545. long cenlen; // 4 bytes
  1546. long cenoff; // 4 bytes
  1547. int comlen; // comment length
  1548. byte[] comment;
  1549. /* members of Zip64 end of central directory locator */
  1550. int diskNum;
  1551. long endpos;
  1552. int disktot;
  1553. void write(OutputStream os, long offset) throws IOException {
  1554. boolean hasZip64 = false;
  1555. long xlen = cenlen;
  1556. long xoff = cenoff;
  1557. if (xlen >= ZIP64_MINVAL) {
  1558. xlen = ZIP64_MINVAL;
  1559. hasZip64 = true;
  1560. }
  1561. if (xoff >= ZIP64_MINVAL) {
  1562. xoff = ZIP64_MINVAL;
  1563. hasZip64 = true;
  1564. }
  1565. int count = centot;
  1566. if (count >= ZIP64_MINVAL32) {
  1567. count = ZIP64_MINVAL32;
  1568. hasZip64 = true;
  1569. }
  1570. if (hasZip64) {
  1571. long off64 = offset;
  1572. //zip64 end of central directory record
  1573. writeInt(os, ZIP64_ENDSIG); // zip64 END record signature
  1574. writeLong(os, ZIP64_ENDHDR - 12); // size of zip64 end
  1575. writeShort(os, 45); // version made by
  1576. writeShort(os, 45); // version needed to extract
  1577. writeInt(os, 0); // number of this disk
  1578. writeInt(os, 0); // central directory start disk
  1579. writeLong(os, centot); // number of directory entires on disk
  1580. writeLong(os, centot); // number of directory entires
  1581. writeLong(os, cenlen); // length of central directory
  1582. writeLong(os, cenoff); // offset of central directory
  1583. //zip64 end of central directory locator
  1584. writeInt(os, ZIP64_LOCSIG); // zip64 END locator signature
  1585. writeInt(os, 0); // zip64 END start disk
  1586. writeLong(os, off64); // offset of zip64 END
  1587. writeInt(os, 1); // total number of disks (?)
  1588. }
  1589. writeInt(os, ENDSIG); // END record signature
  1590. writeShort(os, 0); // number of this disk
  1591. writeShort(os, 0); // central directory start disk
  1592. writeShort(os, count); // number of directory entries on disk
  1593. writeShort(os, count); // total number of directory entries
  1594. writeInt(os, xlen); // length of central directory
  1595. writeInt(os, xoff); // offset of central directory
  1596. if (comment != null) { // zip file comment
  1597. writeShort(os, comment.length);
  1598. writeBytes(os, comment);
  1599. } else {
  1600. writeShort(os, 0);
  1601. }
  1602. }
  1603. }
  1604. // Internal node that links a "name" to its pos in cen table.
  1605. // The node itself can be used as a "key" to lookup itself in
  1606. // the HashMap inodes.
  1607. static class IndexNode {
  1608. byte[] name;
  1609. int hashcode; // node is hashable/hashed by its name
  1610. int pos = -1; // postion in cen table, -1 menas the
  1611. // entry does not exists in zip file
  1612. IndexNode(byte[] name, int pos) {
  1613. name(name);
  1614. this.pos = pos;
  1615. }
  1616. final static IndexNode keyOf(byte[] name) { // get a lookup key;
  1617. return new IndexNode(name, -1);
  1618. }
  1619. final void name(byte[] name) {
  1620. this.name = name;
  1621. this.hashcode = Arrays.hashCode(name);
  1622. }
  1623. final IndexNode as(byte[] name) { // reuse the node, mostly
  1624. name(name); // as a lookup "key"
  1625. return this;
  1626. }
  1627. boolean isDir() {
  1628. return name != null &&
  1629. (name.length == 0 || name[name.length - 1] == '/');
  1630. }
  1631. public boolean equals(Object other) {
  1632. if (!(other instanceof IndexNode)) {
  1633. return false;
  1634. }
  1635. return Arrays.equals(name, ((IndexNode)other).name);
  1636. }
  1637. public int hashCode() {
  1638. return hashcode;
  1639. }
  1640. IndexNode() {}
  1641. IndexNode sibling;
  1642. IndexNode child; // 1st child
  1643. }
  1644. static class Entry extends IndexNode {
  1645. static final int CEN = 1; // entry read from cen
  1646. static final int NEW = 2; // updated contents in bytes or file
  1647. static final int FILECH = 3; // fch update in "file"
  1648. static final int COPY = 4; // copy of a CEN entry
  1649. byte[] bytes; // updated content bytes
  1650. Path file; // use tmp file to store bytes;
  1651. int type = CEN; // default is the entry read from cen
  1652. // entry attributes
  1653. int version;
  1654. int flag;
  1655. int method = -1; // compression method
  1656. long mtime = -1; // last modification time (in DOS time)
  1657. long atime = -1; // last access time
  1658. long ctime = -1; // create time
  1659. long crc = -1; // crc-32 of entry data
  1660. long csize = -1; // compressed size of entry data
  1661. long size = -1; // uncompressed size of entry data
  1662. byte[] extra;
  1663. // cen
  1664. int versionMade;
  1665. int disk;
  1666. int attrs;
  1667. long attrsEx;
  1668. long locoff;
  1669. byte[] comment;
  1670. Entry() {}
  1671. Entry(byte[] name) {
  1672. name(name);
  1673. this.mtime = System.currentTimeMillis();
  1674. this.crc = 0;
  1675. this.size = 0;
  1676. this.csize = 0;
  1677. this.method = METHOD_DEFLATED;
  1678. }
  1679. Entry(byte[] name, int type) {
  1680. this(name);
  1681. this.type = type;
  1682. }
  1683. Entry (Entry e, int type) {
  1684. name(e.name);
  1685. this.version = e.version;
  1686. this.ctime = e.ctime;
  1687. this.atime = e.atime;
  1688. this.mtime = e.mtime;
  1689. this.crc = e.crc;
  1690. this.size = e.size;
  1691. this.csize = e.csize;
  1692. this.method = e.method;
  1693. this.extra = e.extra;
  1694. this.versionMade = e.versionMade;
  1695. this.disk = e.disk;
  1696. this.attrs = e.attrs;
  1697. this.attrsEx = e.attrsEx;
  1698. this.locoff = e.locoff;
  1699. this.comment = e.comment;
  1700. this.type = type;
  1701. }
  1702. Entry (byte[] name, Path file, int type) {
  1703. this(name, type);
  1704. this.file = file;
  1705. this.method = METHOD_STORED;
  1706. }
  1707. int version() throws ZipException {
  1708. if (method == METHOD_DEFLATED)
  1709. return 20;
  1710. else if (method == METHOD_STORED)
  1711. return 10;
  1712. throw new ZipException("unsupported compression method");
  1713. }
  1714. ///////////////////// CEN //////////////////////
  1715. static Entry readCEN(ZipFileSystem zipfs, int pos)
  1716. throws IOException
  1717. {
  1718. return new Entry().cen(zipfs, pos);
  1719. }
  1720. private Entry cen(ZipFileSystem zipfs, int pos)
  1721. throws IOException
  1722. {
  1723. byte[] cen = zipfs.cen;
  1724. if (CENSIG(cen, pos) != CENSIG)
  1725. zerror("invalid CEN header (bad signature)");
  1726. versionMade = CENVEM(cen, pos);
  1727. version = CENVER(cen, pos);
  1728. flag = CENFLG(cen, pos);
  1729. method = CENHOW(cen, pos);
  1730. mtime = dosToJavaTime(CENTIM(cen, pos));
  1731. crc = CENCRC(cen, pos);
  1732. csize = CENSIZ(cen, pos);
  1733. size = CENLEN(cen, pos);
  1734. int nlen = CENNAM(cen, pos);
  1735. int elen = CENEXT(cen, pos);
  1736. int clen = CENCOM(cen, pos);
  1737. disk = CENDSK(cen, pos);
  1738. attrs = CENATT(cen, pos);
  1739. attrsEx = CENATX(cen, pos);
  1740. locoff = CENOFF(cen, pos);
  1741. pos += CENHDR;
  1742. name(Arrays.copyOfRange(cen, pos, pos + nlen));
  1743. pos += nlen;
  1744. if (elen > 0) {
  1745. extra = Arrays.copyOfRange(cen, pos, pos + elen);
  1746. pos += elen;
  1747. readExtra(zipfs);
  1748. }
  1749. if (clen > 0) {
  1750. comment = Arrays.copyOfRange(cen, pos, pos + clen);
  1751. }
  1752. return this;
  1753. }
  1754. int writeCEN(OutputStream os) throws IOException
  1755. {
  1756. int written = CENHDR;
  1757. int version0 = version();
  1758. long csize0 = csize;
  1759. long size0 = size;
  1760. long locoff0 = locoff;
  1761. int elen64 = 0; // extra for ZIP64
  1762. int elenNTFS = 0; // extra for NTFS (a/c/mtime)
  1763. int elenEXTT = 0; // extra for Extended Timestamp
  1764. // confirm size/length
  1765. int nlen = (name != null) ? name.length : 0;
  1766. int elen = (extra != null) ? extra.length : 0;
  1767. int clen = (comment != null) ? comment.length : 0;
  1768. if (csize >= ZIP64_MINVAL) {
  1769. csize0 = ZIP64_MINVAL;
  1770. elen64 += 8; // csize(8)
  1771. }
  1772. if (size >= ZIP64_MINVAL) {
  1773. size0 = ZIP64_MINVAL; // size(8)
  1774. elen64 += 8;
  1775. }
  1776. if (locoff >= ZIP64_MINVAL) {
  1777. locoff0 = ZIP64_MINVAL;
  1778. elen64 += 8; // offset(8)
  1779. }
  1780. if (elen64 != 0)
  1781. elen64 += 4; // header and data sz 4 bytes
  1782. if (atime != -1) {
  1783. if (isWindows) // use NTFS
  1784. elenNTFS = 36; // total 36 bytes
  1785. else // Extended Timestamp otherwise
  1786. elenEXTT = 9; // only mtime in cen
  1787. }
  1788. writeInt(os, CENSIG); // CEN header signature
  1789. if (elen64 != 0) {
  1790. writeShort(os, 45); // ver 4.5 for zip64
  1791. writeShort(os, 45);
  1792. } else {
  1793. writeShort(os, version0); // version made by
  1794. writeShort(os, version0); // version needed to extract
  1795. }
  1796. writeShort(os, flag); // general purpose bit flag
  1797. writeShort(os, method); // compression method
  1798. // last modification time
  1799. writeInt(os, (int)javaToDosTime(mtime));
  1800. writeInt(os, crc); // crc-32
  1801. writeInt(os, csize0); // compressed size
  1802. writeInt(os, size0); // uncompressed size
  1803. writeShort(os, name.length);
  1804. writeShort(os, elen + elen64 + elenNTFS + elenEXTT);
  1805. if (comment != null) {
  1806. writeShort(os, Math.min(clen, 0xffff));
  1807. } else {
  1808. writeShort(os, 0);
  1809. }
  1810. writeShort(os, 0); // starting disk number
  1811. writeShort(os, 0); // internal file attributes (unused)
  1812. writeInt(os, 0); // external file attributes (unused)
  1813. writeInt(os, locoff0); // relative offset of local header
  1814. writeBytes(os, name);
  1815. if (elen64 != 0) {
  1816. writeShort(os, EXTID_ZIP64);// Zip64 extra
  1817. writeShort(os, elen64 - 4); // size of "this" extra block
  1818. if (size0 == ZIP64_MINVAL)
  1819. writeLong(os, size);
  1820. if (csize0 == ZIP64_MINVAL)
  1821. writeLong(os, csize);
  1822. if (locoff0 == ZIP64_MINVAL)
  1823. writeLong(os, locoff);
  1824. }
  1825. if (elenNTFS != 0) {
  1826. // System.out.println("writing NTFS:" + elenNTFS);
  1827. writeShort(os, EXTID_NTFS);
  1828. writeShort(os, elenNTFS - 4);
  1829. writeInt(os, 0); // reserved
  1830. writeShort(os, 0x0001); // NTFS attr tag
  1831. writeShort(os, 24);
  1832. writeLong(os, javaToWinTime(mtime));
  1833. writeLong(os, javaToWinTime(atime));
  1834. writeLong(os, javaToWinTime(ctime));
  1835. }
  1836. if (elenEXTT != 0) {
  1837. writeShort(os, EXTID_EXTT);
  1838. writeShort(os, elenEXTT - 4);
  1839. if (ctime == -1)
  1840. os.write(0x3); // mtime and atime
  1841. else
  1842. os.write(0x7); // mtime, atime and ctime
  1843. writeInt(os, javaToUnixTime(mtime));
  1844. }
  1845. if (extra != null) // whatever not recognized
  1846. writeBytes(os, extra);
  1847. if (comment != null) //TBD: 0, Math.min(commentBytes.length, 0xffff));
  1848. writeBytes(os, comment);
  1849. return CENHDR + nlen + elen + clen + elen64 + elenNTFS + elenEXTT;
  1850. }
  1851. ///////////////////// LOC //////////////////////
  1852. static Entry readLOC(ZipFileSystem zipfs, long pos)
  1853. throws IOException
  1854. {
  1855. return readLOC(zipfs, pos, new byte[1024]);
  1856. }
  1857. static Entry readLOC(ZipFileSystem zipfs, long pos, byte[] buf)
  1858. throws IOException
  1859. {
  1860. return new Entry().loc(zipfs, pos, buf);
  1861. }
  1862. Entry loc(ZipFileSystem zipfs, long pos, byte[] buf)
  1863. throws IOException
  1864. {
  1865. assert (buf.length >= LOCHDR);
  1866. if (zipfs.readFullyAt(buf, 0, LOCHDR , pos) != LOCHDR)
  1867. throw new ZipException("loc: reading failed");
  1868. if (LOCSIG(buf) != LOCSIG)
  1869. throw new ZipException("loc: wrong sig ->"
  1870. + Long.toString(LOCSIG(buf), 16));
  1871. //startPos = pos;
  1872. version = LOCVER(buf);
  1873. flag = LOCFLG(buf);
  1874. method = LOCHOW(buf);
  1875. mtime = dosToJavaTime(LOCTIM(buf));
  1876. crc = LOCCRC(buf);
  1877. csize = LOCSIZ(buf);
  1878. size = LOCLEN(buf);
  1879. int nlen = LOCNAM(buf);
  1880. int elen = LOCEXT(buf);
  1881. name = new byte[nlen];
  1882. if (zipfs.readFullyAt(name, 0, nlen, pos + LOCHDR) != nlen) {
  1883. throw new ZipException("loc: name reading failed");
  1884. }
  1885. if (elen > 0) {
  1886. extra = new byte[elen];
  1887. if (zipfs.readFullyAt(extra, 0, elen, pos + LOCHDR + nlen)
  1888. != elen) {
  1889. throw new ZipException("loc: ext reading failed");
  1890. }
  1891. }
  1892. pos += (LOCHDR + nlen + elen);
  1893. if ((flag & FLAG_DATADESCR) != 0) {
  1894. // Data Descriptor
  1895. Entry e = zipfs.getEntry0(name); // get the size/csize from cen
  1896. if (e == null)
  1897. throw new ZipException("loc: name not found in cen");
  1898. size = e.size;
  1899. csize = e.csize;
  1900. pos += (method == METHOD_STORED ? size : csize);
  1901. if (size >= ZIP64_MINVAL || csize >= ZIP64_MINVAL)
  1902. pos += 24;
  1903. else
  1904. pos += 16;
  1905. } else {
  1906. if (extra != null &&
  1907. (size == ZIP64_MINVAL || csize == ZIP64_MINVAL)) {
  1908. // zip64 ext: must include both size and csize
  1909. int off = 0;
  1910. while (off + 20 < elen) { // HeaderID+DataSize+Data
  1911. int sz = SH(extra, off + 2);
  1912. if (SH(extra, off) == EXTID_ZIP64 && sz == 16) {
  1913. size = LL(extra, off + 4);
  1914. csize = LL(extra, off + 12);
  1915. break;
  1916. }
  1917. off += (sz + 4);
  1918. }
  1919. }
  1920. pos += (method == METHOD_STORED ? size : csize);
  1921. }
  1922. return this;
  1923. }
  1924. int writeLOC(OutputStream os)
  1925. throws IOException
  1926. {
  1927. writeInt(os, LOCSIG); // LOC header signature
  1928. int version = version();
  1929. int nlen = (name != null) ? name.length : 0;
  1930. int elen = (extra != null) ? extra.length : 0;
  1931. int elen64 = 0;
  1932. int elenEXTT = 0;
  1933. if ((flag & FLAG_DATADESCR) != 0) {
  1934. writeShort(os, version()); // version needed to extract
  1935. writeShort(os, flag); // general purpose bit flag
  1936. writeShort(os, method); // compression method
  1937. // last modification time
  1938. writeInt(os, (int)javaToDosTime(mtime));
  1939. // store size, uncompressed size, and crc-32 in data descriptor
  1940. // immediately following compressed entry data
  1941. writeInt(os, 0);
  1942. writeInt(os, 0);
  1943. writeInt(os, 0);
  1944. } else {
  1945. if (csize >= ZIP64_MINVAL || size >= ZIP64_MINVAL) {
  1946. elen64 = 20; //headid(2) + size(2) + size(8) + csize(8)
  1947. writeShort(os, 45); // ver 4.5 for zip64
  1948. } else {
  1949. writeShort(os, version()); // version needed to extract
  1950. }
  1951. writeShort(os, flag); // general purpose bit flag
  1952. writeShort(os, method); // compression method
  1953. // last modification time
  1954. writeInt(os, (int)javaToDosTime(mtime));
  1955. writeInt(os, crc); // crc-32
  1956. if (elen64 != 0) {
  1957. writeInt(os, ZIP64_MINVAL);
  1958. writeInt(os, ZIP64_MINVAL);
  1959. } else {
  1960. writeInt(os, csize); // compressed size
  1961. writeInt(os, size); // uncompressed size
  1962. }
  1963. }
  1964. if (atime != -1 && !isWindows) { // on unix use "ext time"
  1965. if (ctime == -1)
  1966. elenEXTT = 13;
  1967. else
  1968. elenEXTT = 17;
  1969. }
  1970. writeShort(os, name.length);
  1971. writeShort(os, elen + elen64 + elenEXTT);
  1972. writeBytes(os, name);
  1973. if (elen64 != 0) {
  1974. writeShort(os, EXTID_ZIP64);
  1975. writeShort(os, 16);
  1976. writeLong(os, size);
  1977. writeLong(os, csize);
  1978. }
  1979. if (elenEXTT != 0) {
  1980. writeShort(os, EXTID_EXTT);
  1981. writeShort(os, elenEXTT - 4);// size for the folowing data block
  1982. if (ctime == -1)
  1983. os.write(0x3); // mtime and atime
  1984. else
  1985. os.write(0x7); // mtime, atime and ctime
  1986. writeInt(os, javaToUnixTime(mtime));
  1987. writeInt(os, javaToUnixTime(atime));
  1988. if (ctime != -1)
  1989. writeInt(os, javaToUnixTime(ctime));
  1990. }
  1991. if (extra != null) {
  1992. writeBytes(os, extra);
  1993. }
  1994. return LOCHDR + name.length + elen + elen64 + elenEXTT;
  1995. }
  1996. // Data Descriptior
  1997. int writeEXT(OutputStream os)
  1998. throws IOException
  1999. {
  2000. writeInt(os, EXTSIG); // EXT header signature
  2001. writeInt(os, crc); // crc-32
  2002. if (csize >= ZIP64_MINVAL || size >= ZIP64_MINVAL) {
  2003. writeLong(os, csize);
  2004. writeLong(os, size);
  2005. return 24;
  2006. } else {
  2007. writeInt(os, csize); // compressed size
  2008. writeInt(os, size); // uncompressed size
  2009. return 16;
  2010. }
  2011. }
  2012. // read NTFS, UNIX and ZIP64 data from cen.extra
  2013. void readExtra(ZipFileSystem zipfs) throws IOException {
  2014. if (extra == null)
  2015. return;
  2016. int elen = extra.length;
  2017. int off = 0;
  2018. int newOff = 0;
  2019. while (off + 4 < elen) {
  2020. // extra spec: HeaderID+DataSize+Data
  2021. int pos = off;
  2022. int tag = SH(extra, pos);
  2023. int sz = SH(extra, pos + 2);
  2024. pos += 4;
  2025. if (pos + sz > elen) // invalid data
  2026. break;
  2027. switch (tag) {
  2028. case EXTID_ZIP64 :
  2029. if (size == ZIP64_MINVAL) {
  2030. if (pos + 8 > elen) // invalid zip64 extra
  2031. break; // fields, just skip
  2032. size = LL(extra, pos);
  2033. pos += 8;
  2034. }
  2035. if (csize == ZIP64_MINVAL) {
  2036. if (pos + 8 > elen)
  2037. break;
  2038. csize = LL(extra, pos);
  2039. pos += 8;
  2040. }
  2041. if (locoff == ZIP64_MINVAL) {
  2042. if (pos + 8 > elen)
  2043. break;
  2044. locoff = LL(extra, pos);
  2045. pos += 8;
  2046. }
  2047. break;
  2048. case EXTID_NTFS:
  2049. pos += 4; // reserved 4 bytes
  2050. if (SH(extra, pos) != 0x0001)
  2051. break;
  2052. if (SH(extra, pos + 2) != 24)
  2053. break;
  2054. // override the loc field, datatime here is
  2055. // more "accurate"
  2056. mtime = winToJavaTime(LL(extra, pos + 4));
  2057. atime = winToJavaTime(LL(extra, pos + 12));
  2058. ctime = winToJavaTime(LL(extra, pos + 20));
  2059. break;
  2060. case EXTID_EXTT:
  2061. // spec says the Extened timestamp in cen only has mtime
  2062. // need to read the loc to get the extra a/ctime
  2063. byte[] buf = new byte[LOCHDR];
  2064. if (zipfs.readFullyAt(buf, 0, buf.length , locoff)
  2065. != buf.length)
  2066. throw new ZipException("loc: reading failed");
  2067. if (LOCSIG(buf) != LOCSIG)
  2068. throw new ZipException("loc: wrong sig ->"
  2069. + Long.toString(LOCSIG(buf), 16));
  2070. int locElen = LOCEXT(buf);
  2071. if (locElen < 9) // EXTT is at lease 9 bytes
  2072. break;
  2073. int locNlen = LOCNAM(buf);
  2074. buf = new byte[locElen];
  2075. if (zipfs.readFullyAt(buf, 0, buf.length , locoff + LOCHDR + locNlen)
  2076. != buf.length)
  2077. throw new ZipException("loc extra: reading failed");
  2078. int locPos = 0;
  2079. while (locPos + 4 < buf.length) {
  2080. int locTag = SH(buf, locPos);
  2081. int locSZ = SH(buf, locPos + 2);
  2082. locPos += 4;
  2083. if (locTag != EXTID_EXTT) {
  2084. locPos += locSZ;
  2085. continue;
  2086. }
  2087. int flag = CH(buf, locPos++);
  2088. if ((flag & 0x1) != 0) {
  2089. mtime = unixToJavaTime(LG(buf, locPos));
  2090. locPos += 4;
  2091. }
  2092. if ((flag & 0x2) != 0) {
  2093. atime = unixToJavaTime(LG(buf, locPos));
  2094. locPos += 4;
  2095. }
  2096. if ((flag & 0x4) != 0) {
  2097. ctime = unixToJavaTime(LG(buf, locPos));
  2098. locPos += 4;
  2099. }
  2100. break;
  2101. }
  2102. break;
  2103. default: // unknown tag
  2104. System.arraycopy(extra, off, extra, newOff, sz + 4);
  2105. newOff += (sz + 4);
  2106. }
  2107. off += (sz + 4);
  2108. }
  2109. if (newOff != 0 && newOff != extra.length)
  2110. extra = Arrays.copyOf(extra, newOff);
  2111. else
  2112. extra = null;
  2113. }
  2114. }
  2115. private static class ExChannelCloser {
  2116. Path path;
  2117. SeekableByteChannel ch;
  2118. Set<InputStream> streams;
  2119. ExChannelCloser(Path path,
  2120. SeekableByteChannel ch,
  2121. Set<InputStream> streams)
  2122. {
  2123. this.path = path;
  2124. this.ch = ch;
  2125. this.streams = streams;
  2126. }
  2127. }
  2128. // ZIP directory has two issues:
  2129. // (1) ZIP spec does not require the ZIP file to include
  2130. // directory entry
  2131. // (2) all entries are not stored/organized in a "tree"
  2132. // structure.
  2133. // A possible solution is to build the node tree ourself as
  2134. // implemented below.
  2135. private IndexNode root;
  2136. private void addToTree(IndexNode inode, HashSet<IndexNode> dirs) {
  2137. if (dirs.contains(inode)) {
  2138. return;
  2139. }
  2140. IndexNode parent;
  2141. byte[] name = inode.name;
  2142. byte[] pname = getParent(name);
  2143. if (inodes.containsKey(LOOKUPKEY.as(pname))) {
  2144. parent = inodes.get(LOOKUPKEY);
  2145. } else { // pseudo directory entry
  2146. parent = new IndexNode(pname, -1);
  2147. inodes.put(parent, parent);
  2148. }
  2149. addToTree(parent, dirs);
  2150. inode.sibling = parent.child;
  2151. parent.child = inode;
  2152. if (name[name.length -1] == '/')
  2153. dirs.add(inode);
  2154. }
  2155. private void removeFromTree(IndexNode inode) {
  2156. IndexNode parent = inodes.get(LOOKUPKEY.as(getParent(inode.name)));
  2157. IndexNode child = parent.child;
  2158. if (child == inode) {
  2159. parent.child = child.sibling;
  2160. } else {
  2161. IndexNode last = child;
  2162. while ((child = child.sibling) != null) {
  2163. if (child == inode) {
  2164. last.sibling = child.sibling;
  2165. break;
  2166. } else {
  2167. last = child;
  2168. }
  2169. }
  2170. }
  2171. }
  2172. private void buildNodeTree() throws IOException {
  2173. beginWrite();
  2174. try {
  2175. HashSet<IndexNode> dirs = new HashSet<>();
  2176. IndexNode root = new IndexNode(ROOTPATH, -1);
  2177. inodes.put(root, root);
  2178. dirs.add(root);
  2179. for (IndexNode node : inodes.keySet().toArray(new IndexNode[0])) {
  2180. addToTree(node, dirs);
  2181. }
  2182. } finally {
  2183. endWrite();
  2184. }
  2185. }
  2186. }