Let the systems-frames : $\;\mathrm{S}\;$ the system of the Tunnel and $\;\mathrm{S'}\;$ the system of the Train. The train is moving with speed $\;v\;$ from the negatives to the positives of the $\;x-$axis of $\;\mathrm{S}\;$ as in Figure 1.

Now, in our case we have the following two simultaneous events in $\;\mathrm{S}\;$ :
\begin{align}
\mathrm{B} = & \;\text{the back end of the train is at the entrance of the tunnel and the back door}
\tag{01.B}\\
&\; \text{ of the tunnel is closed instantaneously.}
\nonumber\\
\mathrm{F} = & \;\text{the front end of the train is at the exit of the tunnel and the front door}
\tag{01.F}\\
&\; \text{of the tunnel is closed instantaneously.}
\nonumber
\end{align}
So, for the tunnel observer the rest tunnel and the moving train have the same length, let it be $\;L\;$ as in Figure 1. Suppose that the tunnel observer sets his/her space-time origin on event $\;\mathrm{B}$ so that for the coordinates we have :
\begin{align}
\left(x_\mathrm{B}\,,t_\mathrm{B}\right) & =\left(0\,,0\right)
\tag{02.B}\\
\left(x_\mathrm{F}\,,t_\mathrm{F}\right) & =\left(L\,,0\right)
\tag{02.F}
\end{align}
To determine the space-time coordinates of these two events in the system of the Train $\;\mathrm{S'}\;$ we'll use the Lorentz Transformation expressed with differences
\begin{align}
\Delta x' & =\gamma\left(\Delta x-v\,\Delta t\right)
\tag{03.1}\\
\Delta t' & =\gamma\left(\Delta t-\dfrac{\,v\,}{c^2}\Delta x\right)
\tag{03.2}
\end{align}
For convenience, suppose that the train observer sets his/her space-time origin on event $\;\mathrm{B}\;$ also, as in Figure 2, so for the coordinates of the events in the system of the Train $\;\mathrm{S'}\;$ we have :
\begin{align}
\left(x'_\mathrm{B}\,,t'_\mathrm{B}\right) & =\left(0\,,0\right)
\tag{04.B}\\
\left(x'_\mathrm{F}\,,t'_\mathrm{F}\right) & =\left(???\,,???\right)
\tag{04.F}
\end{align}
So
\begin{align}
\Delta x'_\mathrm{FB} & =\gamma\left(\Delta x_\mathrm{FB}-v\,\Delta t_\mathrm{FB}\right) \Longrightarrow x'_\mathrm{F}-x'_\mathrm{B}=\gamma\left[\left(x_\mathrm{F}-x_\mathrm{B}\right)-v\,\left(t_\mathrm{F}-t_\mathrm{B}\right)\right] \Longrightarrow
\nonumber\\
x'_\mathrm{F} & =\gamma\,L
\tag{05.1}\\
\Delta t'_\mathrm{FB} & =\gamma\left(\Delta t_\mathrm{FB}-\dfrac{\,v\,}{c^2}\Delta x_\mathrm{FB}\right)\Longrightarrow t'_\mathrm{F}-t'_\mathrm{B}=\gamma\left[\left(t_\mathrm{F}-t_\mathrm{B}\right)-\dfrac{\,v\,}{c^2}\,\left(x_\mathrm{F}-x_\mathrm{B}\right)\right] \Longrightarrow
\nonumber\\
t'_\mathrm{F} & =-\dfrac{\gamma \,v\,}{c^2}\,L
\tag{05.2}
\end{align}
But, since for the speed $\;v >0$
\begin{equation}
t'_\mathrm{F} =-\dfrac{\gamma \,v\,}{c^2}\,L < 0 = t'_\mathrm{B}
\tag{06}
\end{equation}
that is in the system of the Train $\;\mathrm{S'}\;$ the event $\;\mathrm{F}\;$ happens before event $\;\mathrm{B}\;$ by a time interval
\begin{equation}
\vert \Delta t'_\mathrm{FB} \vert =\vert t'_\mathrm{F}-t'_\mathrm{B} \vert=\dfrac{\gamma \,v\,}{c^2}\,L
\tag{07}
\end{equation}
Since in the system of the Train $\;\mathrm{S'}\;$ the front end and the back end of the train stand at rest on the coordinates $\;x'_\mathrm{F} =\gamma\,L\;$ and $\;x'_\mathrm{B} =0\;$ respectively, for the length of the train in its rest frame we have as expected
\begin{equation}
\text{Length of the train in its rest frame}=\vert \Delta x'_\mathrm{FB} \vert =\vert x'_\mathrm{F}-x'_\mathrm{B} \vert=\gamma\,L
\tag{08}
\end{equation}
Now, a train observer on the back end of the train at the time moment that the event $\;\mathrm{F}\;$ is happening, that is on $\;x'_\mathrm{B} =0\;$ at $\;t'_\mathrm{F}=-\gamma\,v\,L/c^2 \;$, will meet the back entrance of the tunnel at time moment $\;t'_\mathrm{B}=0\;$, that is after the time interval $\;\vert \Delta t'_\mathrm{FB}\vert \;$ of equation (07). But, since the tunnel is moving with speed $\;v\;$ from the positives to the negatives of the $\;x'-$axis, the back end of the train and the back entrance of the tunnel at time moment $\;t'_\mathrm{F}\;$ are apart at a distance
\begin{equation}
v\, \vert \Delta t'_\mathrm{FB} \vert =\dfrac{\gamma \,v^2\,}{c^2}\,L= \gamma \left(1-\dfrac{1}{\gamma^2}\right)L= \left(\gamma-\dfrac{1}{\gamma}\right)L
\tag{09}
\end{equation}
This is the length of the back portion of the train outside the tunnel at the time moment $\;t'_\mathrm{F}$. This is also the length of the front portion of the train outside the tunnel at the time moment $\;t'_\mathrm{B}$, see Figure 2. On the other hand from this same Figure we have
\begin{equation}
\text{Length of the tunnel in the Train frame }\mathrm{S'} =\gamma\,L-\left(\gamma-\dfrac{1}{\gamma}\right)L=\dfrac{L}{\gamma}
\tag{10}
\end{equation}
as expected.
For a space-time Diagram see Figure 3.

Numerical Example
Let
\begin{align}
\dfrac{\,v\;}{c} & =0.60
\tag{NE.1}\\
L & =100\,m
\tag{NE.2}
\end{align}
then
\begin{equation}
\gamma = 1.25\,,\quad \dfrac{\,1\;}{\gamma}=0.80
\tag{NE.3}
\end{equation}
The length of the train in its rest frame is $\;\gamma\,L = 1.25\times 100\,m= 125\,m\;$ contracted to $\;L = 100\,m\;$ in the tunnel frame.
The length of the tunnel in its rest frame is $\;L = 100\,m\;$ contracted to $\;L/\gamma = 80\,m\;$ in the train frame.
The length of the portion of the train in its rest frame outside the tunnel is $\;(\gamma-\gamma^{-1})L=(1.25-0.80)\times 100\,m= 45 \,m $.
The two simultaneous events in $\;\mathrm{S}\;$ are in $\;\mathrm{S'}\;$ apart by a time interval $\;\gamma (v / c)(L/c)=1.25\times 0.60 \times 10^2\,m/(3\cdot 10^8\,m/sec) = 0.25\cdot 10^{-6}sec =0.25\mu s$.