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.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. ZipArchiveOutputStream out = new ZipArchiveOutputStream(s);
  46. int compressionLevel = getCompressionLevel(o);
  47. if (compressionLevel != -1) {
  48. out.setLevel(compressionLevel);
  49. }
  50. return applyFormatOptions(out, o);
  51. }
  52. /** {@inheritDoc} */
  53. @Override
  54. public void putEntry(ArchiveOutputStream out,
  55. ObjectId tree, String path, FileMode mode, ObjectLoader loader)
  56. throws IOException {
  57. // ZipArchiveEntry detects directories by checking
  58. // for '/' at the end of the filename.
  59. if (path.endsWith("/") && mode != FileMode.TREE) //$NON-NLS-1$
  60. throw new IllegalArgumentException(MessageFormat.format(
  61. ArchiveText.get().pathDoesNotMatchMode, path, mode));
  62. if (!path.endsWith("/") && mode == FileMode.TREE) //$NON-NLS-1$
  63. path = path + "/"; //$NON-NLS-1$
  64. final ZipArchiveEntry entry = new ZipArchiveEntry(path);
  65. if (tree instanceof RevCommit) {
  66. long t = ((RevCommit) tree).getCommitTime() * 1000L;
  67. entry.setTime(t);
  68. }
  69. if (mode == FileMode.TREE) {
  70. out.putArchiveEntry(entry);
  71. out.closeArchiveEntry();
  72. return;
  73. }
  74. if (mode == FileMode.REGULAR_FILE) {
  75. // ok
  76. } else if (mode == FileMode.EXECUTABLE_FILE
  77. || mode == FileMode.SYMLINK) {
  78. entry.setUnixMode(mode.getBits());
  79. } else {
  80. // Unsupported mode (e.g., GITLINK).
  81. throw new IllegalArgumentException(MessageFormat.format(
  82. ArchiveText.get().unsupportedMode, mode));
  83. }
  84. entry.setSize(loader.getSize());
  85. out.putArchiveEntry(entry);
  86. loader.copyTo(out);
  87. out.closeArchiveEntry();
  88. }
  89. /** {@inheritDoc} */
  90. @Override
  91. public Iterable<String> suffixes() {
  92. return SUFFIXES;
  93. }
  94. /** {@inheritDoc} */
  95. @Override
  96. public boolean equals(Object other) {
  97. return (other instanceof ZipFormat);
  98. }
  99. /** {@inheritDoc} */
  100. @Override
  101. public int hashCode() {
  102. return getClass().hashCode();
  103. }
  104. }