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

ZipEncoding.java 3.3 kB

11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing,
  13. * software distributed under the License is distributed on an
  14. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. * KIND, either express or implied. See the License for the
  16. * specific language governing permissions and limitations
  17. * under the License.
  18. */
  19. package org.apache.tools.zip;
  20. import java.io.IOException;
  21. import java.nio.ByteBuffer;
  22. /**
  23. * An interface for encoders that do a pretty encoding of ZIP
  24. * filenames.
  25. *
  26. * <p>There are mostly two implementations, one that uses java.nio
  27. * {@link java.nio.charset.Charset Charset} and one implementation,
  28. * which copes with simple 8 bit charsets, because java-1.4 did not
  29. * support Cp437 in java.nio.</p>
  30. *
  31. * <p>The main reason for defining an own encoding layer comes from
  32. * the problems with {@link java.lang.String#getBytes(String)
  33. * String.getBytes}, which encodes unknown characters as ASCII
  34. * quotation marks ('?'). Quotation marks are per definition an
  35. * invalid filename on some operating systems like Windows, which
  36. * leads to ignored ZIP entries.</p>
  37. *
  38. * <p>All implementations should implement this interface in a
  39. * reentrant way.</p>
  40. */
  41. public interface ZipEncoding {
  42. /**
  43. * Check, whether the given string may be losslessly encoded using this
  44. * encoding.
  45. *
  46. * @param name A filename or ZIP comment.
  47. * @return Whether the given name may be encoded with out any losses.
  48. */
  49. boolean canEncode(String name);
  50. /**
  51. * Encode a filename or a comment to a byte array suitable for
  52. * storing it to a serialized zip entry.
  53. *
  54. * <p>Examples for CP 437 (in pseudo-notation, right hand side is
  55. * C-style notation):</p>
  56. * <pre>
  57. * encode("\u20AC_for_Dollar.txt") = "%U20AC_for_Dollar.txt"
  58. * encode("\u00D6lf\u00E4sser.txt") = "\231lf\204sser.txt"
  59. * </pre>
  60. *
  61. * @param name A filename or ZIP comment.
  62. * @return A byte buffer with a backing array containing the
  63. * encoded name. Unmappable characters or malformed
  64. * character sequences are mapped to a sequence of utf-16
  65. * words encoded in the format <code>%Uxxxx</code>. It is
  66. * assumed, that the byte buffer is positioned at the
  67. * beginning of the encoded result, the byte buffer has a
  68. * backing array and the limit of the byte buffer points
  69. * to the end of the encoded result.
  70. * @throws IOException if something goes wrong
  71. */
  72. ByteBuffer encode(String name) throws IOException;
  73. /**
  74. * @param data The byte values to decode.
  75. * @return The decoded string.
  76. * @throws IOException if something goes wrong
  77. */
  78. String decode(byte[] data) throws IOException;
  79. }