You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ZipFormat.java 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /*
  2. * Copyright (C) 2012 Google Inc. and others
  3. *
  4. * This program and the accompanying materials are made available under the
  5. * terms of the Eclipse Distribution License v. 1.0 which is available at
  6. * https://www.eclipse.org/org/documents/edl-v10.php.
  7. *
  8. * SPDX-License-Identifier: BSD-3-Clause
  9. */
  10. package org.eclipse.jgit.archive;
  11. import java.io.IOException;
  12. import java.io.OutputStream;
  13. import java.text.MessageFormat;
  14. import java.util.Arrays;
  15. import java.util.Collections;
  16. import java.util.List;
  17. import java.util.Map;
  18. import org.apache.commons.compress.archivers.ArchiveOutputStream;
  19. import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
  20. import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream;
  21. import org.eclipse.jgit.api.ArchiveCommand;
  22. import org.eclipse.jgit.archive.internal.ArchiveText;
  23. import org.eclipse.jgit.lib.FileMode;
  24. import org.eclipse.jgit.lib.ObjectId;
  25. import org.eclipse.jgit.lib.ObjectLoader;
  26. import org.eclipse.jgit.revwalk.RevCommit;
  27. /**
  28. * PKWARE's ZIP format.
  29. */
  30. public final class ZipFormat extends BaseFormat implements
  31. ArchiveCommand.Format<ArchiveOutputStream> {
  32. private static final List<String> SUFFIXES = Collections
  33. .unmodifiableList(Arrays.asList(".zip")); //$NON-NLS-1$
  34. /** {@inheritDoc} */
  35. @Override
  36. public ArchiveOutputStream createArchiveOutputStream(OutputStream s)
  37. throws IOException {
  38. return createArchiveOutputStream(s,
  39. Collections.<String, Object> emptyMap());
  40. }
  41. /** {@inheritDoc} */
  42. @Override
  43. public ArchiveOutputStream createArchiveOutputStream(OutputStream s,
  44. Map<String, Object> o) throws IOException {
  45. return applyFormatOptions(new ZipArchiveOutputStream(s), o);
  46. }
  47. /** {@inheritDoc} */
  48. @Override
  49. public void putEntry(ArchiveOutputStream out,
  50. ObjectId tree, String path, FileMode mode, ObjectLoader loader)
  51. throws IOException {
  52. // ZipArchiveEntry detects directories by checking
  53. // for '/' at the end of the filename.
  54. if (path.endsWith("/") && mode != FileMode.TREE) //$NON-NLS-1$
  55. throw new IllegalArgumentException(MessageFormat.format(
  56. ArchiveText.get().pathDoesNotMatchMode, path, mode));
  57. if (!path.endsWith("/") && mode == FileMode.TREE) //$NON-NLS-1$
  58. path = path + "/"; //$NON-NLS-1$
  59. final ZipArchiveEntry entry = new ZipArchiveEntry(path);
  60. if (tree instanceof RevCommit) {
  61. long t = ((RevCommit) tree).getCommitTime() * 1000L;
  62. entry.setTime(t);
  63. }
  64. if (mode == FileMode.TREE) {
  65. out.putArchiveEntry(entry);
  66. out.closeArchiveEntry();
  67. return;
  68. }
  69. if (mode == FileMode.REGULAR_FILE) {
  70. // ok
  71. } else if (mode == FileMode.EXECUTABLE_FILE
  72. || mode == FileMode.SYMLINK) {
  73. entry.setUnixMode(mode.getBits());
  74. } else {
  75. // Unsupported mode (e.g., GITLINK).
  76. throw new IllegalArgumentException(MessageFormat.format(
  77. ArchiveText.get().unsupportedMode, mode));
  78. }
  79. entry.setSize(loader.getSize());
  80. out.putArchiveEntry(entry);
  81. loader.copyTo(out);
  82. out.closeArchiveEntry();
  83. }
  84. /** {@inheritDoc} */
  85. @Override
  86. public Iterable<String> suffixes() {
  87. return SUFFIXES;
  88. }
  89. /** {@inheritDoc} */
  90. @Override
  91. public boolean equals(Object other) {
  92. return (other instanceof ZipFormat);
  93. }
  94. /** {@inheritDoc} */
  95. @Override
  96. public int hashCode() {
  97. return getClass().hashCode();
  98. }
  99. }