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.

LoadPasswordProtectedXlsx.java 3.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /*
  2. * ====================================================================
  3. * Licensed to the Apache Software Foundation (ASF) under one or more
  4. * contributor license agreements. See the NOTICE file distributed with
  5. * this work for additional information regarding copyright ownership.
  6. * The ASF licenses this file to You under the Apache License, Version 2.0
  7. * (the "License"); you may not use this file except in compliance with
  8. * 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, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. * ====================================================================
  18. */
  19. package org.apache.poi.examples.xssf.usermodel;
  20. import java.io.FileInputStream;
  21. import java.io.InputStream;
  22. import org.apache.poi.examples.util.TempFileUtils;
  23. import org.apache.poi.openxml4j.opc.OPCPackage;
  24. import org.apache.poi.poifs.crypt.Decryptor;
  25. import org.apache.poi.poifs.crypt.EncryptionInfo;
  26. import org.apache.poi.poifs.crypt.temp.AesZipFileZipEntrySource;
  27. import org.apache.poi.poifs.filesystem.POIFSFileSystem;
  28. import org.apache.poi.xssf.usermodel.XSSFWorkbook;
  29. /**
  30. * An example that loads a password protected workbook and counts the sheets.
  31. * <ul>
  32. * <li>The example demonstrates that all temp files are removed.
  33. * <li>{@code AesZipFileZipEntrySource} is used to ensure that temp files are encrypted.
  34. * </ul>
  35. */
  36. @SuppressWarnings({"java:S106","java:S4823","java:S1192"})
  37. public final class LoadPasswordProtectedXlsx {
  38. private LoadPasswordProtectedXlsx() {}
  39. public interface EncryptionHandler {
  40. void handle(final InputStream inputStream) throws Exception;
  41. }
  42. public static void main(String[] args) throws Exception {
  43. execute(args, LoadPasswordProtectedXlsx::printSheetCount);
  44. }
  45. public static void execute(String[] args, EncryptionHandler handler) throws Exception {
  46. if(args.length != 2) {
  47. throw new IllegalArgumentException("Expected 2 params: filename and password");
  48. }
  49. TempFileUtils.checkTempFiles();
  50. String filename = args[0];
  51. String password = args[1];
  52. try (FileInputStream fis = new FileInputStream(filename);
  53. POIFSFileSystem fs = new POIFSFileSystem(fis)) {
  54. EncryptionInfo info = new EncryptionInfo(fs);
  55. Decryptor d = Decryptor.getInstance(info);
  56. if (!d.verifyPassword(password)) {
  57. throw new RuntimeException("incorrect password");
  58. }
  59. try (InputStream unencryptedStream = d.getDataStream(fs)) {
  60. handler.handle(unencryptedStream);
  61. }
  62. }
  63. TempFileUtils.checkTempFiles();
  64. }
  65. private static void printSheetCount(final InputStream inputStream) throws Exception {
  66. try (AesZipFileZipEntrySource source = AesZipFileZipEntrySource.createZipEntrySource(inputStream);
  67. OPCPackage pkg = OPCPackage.open(source);
  68. XSSFWorkbook workbook = new XSSFWorkbook(pkg)) {
  69. System.out.println("sheet count: " + workbook.getNumberOfSheets());
  70. }
  71. }
  72. }