-
Notifications
You must be signed in to change notification settings - Fork 1
/
repack.bat
139 lines (119 loc) · 4.13 KB
/
repack.bat
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
@echo off
rem DOCX repacking script using 7zip
rem AUTHOR: grzegorz.adam.kowalski@outlook.com, grzegorz.adam.kowalski@gmail.com
rem Path to 7-zip executable
set COMPRESSOR=dependencies\7z-22.01\7za.exe
rem Path to Touch utility
set TOUCH_UTILITY=dependencies\touch.exe
set TOUCH_OPTIONS=-c -r %1.bak %1
rem Path to PNG optimizer
set PNG_OPTIMIZER=dependencies\optipng-0.7.7-win32\optipng.exe
rem Path to JPEG optimizer executable
set JPG_OPTIMIZER=dependencies\jpegtran.exe
rem Folder name with leading backslash
set TEMP_SUBFOLDER=\Docx-Repack
if not exist "%1" goto label_no_input
:decompress
if not exist "%COMPRESSOR%" goto label_no_compressor
"%COMPRESSOR%" x -y -tzip %1 -o%TEMP%%TEMP_SUBFOLDER%
:make_backup
if exist %1.bak del %1.bak
move %1 %1.bak
:optimize_png
if not exist "%PNG_OPTIMIZER%" goto optimize_jpeg
rem Media folder for DOCX files
set MEDIA_FOLDER=%TEMP%%TEMP_SUBFOLDER%\word\media
if exist "%MEDIA_FOLDER%" (
FOR /F %%j IN ('dir /B %MEDIA_FOLDER%\*.png') DO (
echo "%PNG_OPTIMIZER%" --preserve -o5 %MEDIA_FOLDER%\%%j
"%PNG_OPTIMIZER%" --preserve -o5 %MEDIA_FOLDER%\%%j
)
)
rem Media folder for PPTX files
set MEDIA_FOLDER=%TEMP%%TEMP_SUBFOLDER%\ppt\media
if exist "%MEDIA_FOLDER%" (
FOR /F %%j IN ('dir /B %MEDIA_FOLDER%\*.png') DO (
echo "%PNG_OPTIMIZER%" --preserve -o5 %MEDIA_FOLDER%\%%j
"%PNG_OPTIMIZER%" --preserve -o5 %MEDIA_FOLDER%\%%j
)
)
rem Media folder for XLSX files
set MEDIA_FOLDER=%TEMP%%TEMP_SUBFOLDER%\xl\media
if exist "%MEDIA_FOLDER%" (
FOR /F %%j IN ('dir /B %MEDIA_FOLDER%\*.png') DO (
echo "%PNG_OPTIMIZER%" --preserve -o5 %MEDIA_FOLDER%\%%j
"%PNG_OPTIMIZER%" --preserve -o5 %MEDIA_FOLDER%\%%j
)
)
rem Media folder for ODT files
set MEDIA_FOLDER=%TEMP%%TEMP_SUBFOLDER%\media
if exist "%MEDIA_FOLDER%" (
FOR /F %%j IN ('dir /B %MEDIA_FOLDER%\*.png') DO (
echo "%PNG_OPTIMIZER%" --preserve -o5 %MEDIA_FOLDER%\%%j
"%PNG_OPTIMIZER%" --preserve -o5 %MEDIA_FOLDER%\%%j
)
)
rem Thumbnail folder for ODS files
set MEDIA_FOLDER=%TEMP%%TEMP_SUBFOLDER%\Thumbnails
if exist "%MEDIA_FOLDER%" (
FOR /F %%j IN ('dir /B %MEDIA_FOLDER%\*.png') DO (
echo "%PNG_OPTIMIZER%" --preserve -o5 %MEDIA_FOLDER%\%%j
"%PNG_OPTIMIZER%" --preserve -o5 %MEDIA_FOLDER%\%%j
)
)
:optimize_jpeg
if not exist "%JPG_OPTIMIZER%" goto compress
rem Media folder for DOCX files
set MEDIA_FOLDER=%TEMP%%TEMP_SUBFOLDER%\word\media
if exist "%MEDIA_FOLDER%" (
FOR /F %%j IN ('dir /B %MEDIA_FOLDER%\*.jp*') DO (
echo %JPG_OPTIMIZER% -optimize %MEDIA_FOLDER%\%%j %MEDIA_FOLDER%\%%j
%JPG_OPTIMIZER% -optimize %MEDIA_FOLDER%\%%j %MEDIA_FOLDER%\%%j
)
)
rem Media folder for PPTX files
set MEDIA_FOLDER=%TEMP%%TEMP_SUBFOLDER%\ppt\media
if exist "%MEDIA_FOLDER%" (
FOR /F %%j IN ('dir /B %MEDIA_FOLDER%\*.jp*') DO (
echo %JPG_OPTIMIZER% -optimize %MEDIA_FOLDER%\%%j %MEDIA_FOLDER%\%%j
%JPG_OPTIMIZER% -optimize %MEDIA_FOLDER%\%%j %MEDIA_FOLDER%\%%j
)
)
rem Media folder for XLSX files
set MEDIA_FOLDER=%TEMP%%TEMP_SUBFOLDER%\xl\media
if exist "%MEDIA_FOLDER%" (
FOR /F %%j IN ('dir /B %MEDIA_FOLDER%\*.jp*') DO (
echo %JPG_OPTIMIZER% -optimize %MEDIA_FOLDER%\%%j %MEDIA_FOLDER%\%%j
%JPG_OPTIMIZER% -optimize %MEDIA_FOLDER%\%%j %MEDIA_FOLDER%\%%j
)
)
rem Media folder for ODT files
set MEDIA_FOLDER=%TEMP%%TEMP_SUBFOLDER%\media
if exist "%MEDIA_FOLDER%" (
FOR /F %%j IN ('dir /B %MEDIA_FOLDER%\*.jp*') DO (
echo %JPG_OPTIMIZER% -optimize %MEDIA_FOLDER%\%%j %MEDIA_FOLDER%\%%j
%JPG_OPTIMIZER% -optimize %MEDIA_FOLDER%\%%j %MEDIA_FOLDER%\%%j
)
)
rem Thumbnail folder for ODS files
set MEDIA_FOLDER=%TEMP%%TEMP_SUBFOLDER%\Thumbnails
if exist "%MEDIA_FOLDER%" (
FOR /F %%j IN ('dir /B %MEDIA_FOLDER%\*.jp*') DO (
echo %JPG_OPTIMIZER% -optimize %MEDIA_FOLDER%\%%j %MEDIA_FOLDER%\%%j
%JPG_OPTIMIZER% -optimize %MEDIA_FOLDER%\%%j %MEDIA_FOLDER%\%%j
)
)
:compress
"%COMPRESSOR%" a -tzip %1 -r %TEMP%%TEMP_SUBFOLDER%\* -mx=9 -mfb=258 -mm=Deflate -mpass=15
:copy_modification_date
if exist "%TOUCH_UTILITY%" "%TOUCH_UTILITY%" %TOUCH_OPTIONS%
:cleanup
rmdir /S /Q %TEMP%%TEMP_SUBFOLDER%
goto label_exit
:label_no_input
echo File "%1" does not exist!
goto label_exit
:label_no_compressor
echo No archiving utility at this path: "%COMPRESSOR%"!
goto label_exit
:label_exit