opc_trim.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /**
  2. Copyright (c) 2010, Florian Reuter
  3. All rights reserved.
  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. * Redistributions of source code must retain the above copyright
  8. notice, this list of conditions and the following disclaimer.
  9. * Redistributions in binary form must reproduce the above copyright
  10. notice, this list of conditions and the following disclaimer in
  11. the documentation and/or other materials provided with the
  12. distribution.
  13. * Neither the name of Florian Reuter nor the names of its contributors
  14. may be used to endorse or promote products derived from this
  15. software without specific prior written permission.
  16. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  17. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  18. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  19. FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  20. COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  21. INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  22. BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  23. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  24. CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  25. STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  26. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  27. OF THE POSSIBILITY OF SUCH DAMAGE.
  28. */
  29. /*
  30. Trim an OPC container, i.e. remove all unneeded bytes and make the container as small as possible.
  31. [Content Types].xml and all _rels will be rewritten.
  32. Ussage:
  33. opc_trim FILENAME
  34. Sample:
  35. opc_trim OOXMLI1.docx
  36. */
  37. #include <opc/opc.h>
  38. #include <stdio.h>
  39. #include <time.h>
  40. #ifdef WIN32
  41. #include <crtdbg.h>
  42. #endif
  43. int main( int argc, const char* argv[] )
  44. {
  45. #ifdef WIN32
  46. _CrtSetDbgFlag (_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);
  47. #endif
  48. time_t start_time=time(NULL);
  49. opc_error_t err=OPC_ERROR_NONE;
  50. if (OPC_ERROR_NONE==opcInitLibrary() && 2==argc) {
  51. opcContainer *c=NULL;
  52. if (NULL!=(c=opcContainerOpen(_X(argv[1]), OPC_OPEN_READ_WRITE, NULL, NULL))) {
  53. opcContainerDump(c, stdout);
  54. opcContainerClose(c, OPC_CLOSE_TRIM);
  55. } else {
  56. printf("ERROR: \"%s\" could not be opened.\n", argv[1]);
  57. err=OPC_ERROR_STREAM;
  58. }
  59. opcFreeLibrary();
  60. } else if (2==argc) {
  61. printf("ERROR: initialization of libopc failed.\n");
  62. err=OPC_ERROR_STREAM;
  63. } else {
  64. printf("opc_dump FILENAME.\n\n");
  65. printf("Sample: opc_dump test.docx\n");
  66. }
  67. time_t end_time=time(NULL);
  68. fprintf(stderr, "time %.2lfsec\n", difftime(end_time, start_time));
  69. #ifdef WIN32
  70. OPC_ASSERT(!_CrtDumpMemoryLeaks());
  71. #endif
  72. return (OPC_ERROR_NONE==err?0:3);
  73. }