Get a value from xml file inside a war file without extracting - linux

I want to read a line from a jsp file present in my war file. The path of the jsp file is always the same, so I know where exactly to look for. How do I grep the line from the jsp inside the war file, without extracting the whole war file? I don't want to extract because it would take at least couple of minutes to finish, and I just want to get the version value (1.2.3.210) from that line. This is how the structure looks like -
war file name --> final.war
jsp location inside the war file --> folder1/folder2/version.jsp
line inside the jsp file --> <param><value>version=1.2.3.210</value></param>

A .war file is just a zip file, so we can use unzip to do what you want. Let's say I have a file sample.war with the following content:
Archive: ../sample.war
Length Date Time Name
--------- ---------- ----- ----
376 07-30-2007 16:26 hello.jsp
0 07-30-2007 16:26 images/
1441 07-30-2007 16:26 images/tomcat.gif
636 07-30-2007 16:26 index.html
0 07-30-2007 17:18 META-INF/
106 07-30-2007 17:18 META-INF/MANIFEST.MF
0 07-30-2007 16:26 WEB-INF/
0 07-30-2007 16:26 WEB-INF/lib/
813 07-30-2007 16:26 WEB-INF/web.xml
0 07-30-2007 16:26 WEB-INF/classes/
0 07-30-2007 16:26 WEB-INF/classes/mypackage/
1480 07-30-2007 16:26 WEB-INF/classes/mypackage/Hello.class
--------- -------
4852 12 files
I can use unzip to extract the file index.html to stdout like this:
unzip -p sample.war index.html
To get a particular line from the output, you can just pipe it into sed. For example, to extract the line containing <title>:
$ unzip -p sample.war index.html | sed -n '/<title>/p'
<title>Sample "Hello, World" Application</title>

This worked for me. I did not have to install jar, as it was already available in CentOS
jar -xf final.war folder1/folder2/version.jsp

Related

file zip/tar in linux at specific location

I want to zip a set of directories and files on my centos 8 VM.
There are 3 directories and 1 file which I want to zip in such a way that only env.conf file will move to /etc/env.txt after unzipping it and remaining directories will be unzipped at current location.
Is there any way to achieve this.
drwxr-xr-x. 9 root root 114 Feb 25 12:40 config
-rw-r--r--. 1 root root 340 Feb 25 09:01 env.conf
drwxr-xr-x. 9 root root 4096 Feb 28 05:11 platform
drwxr-xr-x. 2 root root 135 Feb 28 07:49 install
I don't think this is possible. in fact this is considered a vulnerability if you could do that.
Imagine you download a zip file from some website. and after you unzip it in a temp folder. It registers itself as a service by writing a file in /etc somewhere, and gets control over your pc.
Example: zip-slip
You could however create a one-liner that extracts and moves the file wherever you want like this:
unzip <filename> && mv env.conf /etc/env.txt

java -xvf test.jar not working

i tried to extract the test.jar
command is executing successfully but no output.
user#host:home/test->ll
drwxr-xr-x 107 user abc 6040 Apr 4 09:55 ..
drwxr-xr-x 2 user abc 26 Apr 4 10:06 .
-rw-r--r-- 1 user abc 51241 Apr 4 10:06 test.jar
user#host:home/test->jar -xvf test.jar
user#host:home/test->ll
total 262
drwxr-xr-x 107 user abc 6040 Apr 4 09:55 ..
drwxr-xr-x 2 user abc 26 Apr 4 10:06 .
-rw-r--r-- 1 user abc 51241 Apr 4 10:06 test.jar
Kindly help me to resolve this
Actual Requirement:
Need to extract and access a resource in jar file.
According to Oracle Java toutorials:
https://docs.oracle.com/javase/tutorial/deployment/jar/unpack.html
You should unpack it with:
jar xfv test.jar
Without '-' sign.
x option indicates that you want to extract files from the JAR archive.
f options indicates that the JAR file from which files are to be extracted is specified on the command line, rather than through stdin.
v is verbose
try to this command also unzip test.jar and below commands help you view some file without extract all the files.
unzip -q -c test.jar META-INF/MANIFEST.MF

Extract/See content of a specific file inside a .war file

I am trying to view the content of a file ( in linux )that is inside a .war file without having to extract the war file. For example inside the war file i am trying to see the content of a file called aa.txt and which is under /path/here/ inside the war file.
jar has x option for that:
$ jar -help 2>&1 | grep extract
-x extract named (or all) files from archive
Let's try it:
$ jar tvf target/my.war | grep test-data
1811 Tue Jun 21 19:34:50 CEST 2016 WEB-INF/classes/test/spring/test-data.properties
$ jar xvf target/my.war WEB-INF/classes/test/spring/test-data.properties
inflated: WEB-INF/classes/test/spring/test-data.properties
$ ls -l WEB-INF/classes/test/spring/test-data.properties
-rw-rw-r--. 1 coder coder 1811 Jun 21 19:34 WEB-INF/classes/test/spring/test-data.properties

tar: can't change directories to <dir>/<file>: not a directory

I need to tar this dir - totar:
$ ls -l totar/
total 61388
292 -rw-r--r-- 1 wasext was 298867 Apr 13 16:44 application.2015-01-19.0.log
108 -rw-r--r-- 1 wasext was 109623 Apr 14 13:32 application.2015-01-20.0.log
788 -rw-r--r-- 1 wasext was 805468 Apr 14 13:32 application.2015-01-21.0.log
1080 -rw-r--r-- 1 wasext was 1104861 Apr 14 13:32 application.2015-01-22.0.log
2052 -rw-r--r-- 1 wasext was 2098782 Apr 14 13:32 application.2015-01-23.0.log
4 -rw-r--r-- 1 wasext was 455 Apr 14 13:32 application.2015-01-24.0.log
21076 -rw-r--r-- 1 wasext was 21581073 Apr 14 13:32 application.2015-01-26.0.log
9296 -rw-r--r-- 1 wasext was 9519026 Apr 14 13:32 application.2015-01-27.0.log
17912 -rw-r--r-- 1 wasext was 18341302 Apr 14 13:32 application.2015-01-28.0.log
8780 -rw-r--r-- 1 wasext was 8989019 Apr 14 13:32 application.2015-01-29.0.log
I created tarball file with parameter "-C":
$ tar cvf t.tar -C totar/*
tar: can't change directories to totar/application.2015-01-19.0.log: Not a directory
a totar/application.2015-01-20.0.log 215 blocks
a totar/application.2015-01-21.0.log 1574 blocks
a totar/application.2015-01-22.0.log 2158 blocks
a totar/application.2015-01-23.0.log 4100 blocks
a totar/application.2015-01-24.0.log 1 blocks
a totar/application.2015-01-26.0.log 42151 blocks
a totar/application.2015-01-27.0.log 18592 blocks
a totar/application.2015-01-28.0.log 35823 blocks
a totar/application.2015-01-29.0.log 17557 blocks
I got this error:
tar: can't change directories to totar/application.2015-01-19.0.log: Not a directory
In created tar file is missing file: totar/application.2015-01-19.0.log:
$ tar tf t.tar
totar/application.2015-01-20.0.log
totar/application.2015-01-21.0.log
totar/application.2015-01-22.0.log
totar/application.2015-01-23.0.log
totar/application.2015-01-24.0.log
totar/application.2015-01-26.0.log
totar/application.2015-01-27.0.log
totar/application.2015-01-28.0.log
totar/application.2015-01-29.0.log
Can you help me where I made mistake?
With the -C option to tar, you are telling tar to change to that directory first before adding files to the archive. This is very convenient to prevent having to change to that directory to begin with. Say you are in your $HOME directory and want to archive the files in /path/to/totar, then you simply give the -C option to the directory above totar and target directory. If you want to archive /path/to/totar, then you want to:
tar -C /path/to cvf somearchive.tar totar
Note the files spec above totar is a directory name not a filename and will include all files in the totar directory. You do not include wildcards, but you can include specific filenames. To backup only some files in /path/to/totar:
tar -C /path/to/totar cvf somearchive.tar filename
to only filename in the totar directory. (wildcards are not allowed)
I'll see if I can make it clearer: When you enter anything with *, your shell immediately expands it to a list of files that match the patten.
So say you have a simpler directory: totar contains just 3 files: foo, bar, and foobar
When you then type:
tar cvf t.tar -C totar/*
It gets expanded to
tar cvf t.tar -C totar/foo totar/bar totar/foobar
Since -C takes a parameter, tar interprets the next argument, totar/foo, as the parameter of -C. And only totar/bar and totar/foobar remain as the files to be archived.
Do you really need the -C? Because I suggest leaving the -C out completely
$ tar cvf t.tar totar/*
Update:
If you want to retain the behaviour of -C, I strongly suggest that you have a look at #charles-duffy 's comments below.
Cheers

KMZ do not show of my icons

Earlier, I tried to put the file on the host kml. My question here Not showing the path in KML. Now I created new file kmz on the recommendations of Google. File here: http://tourist-sweden.se/transport/map/sthlm/t-11-1.kmz . Calling map: http://tourist-sweden.se/transport/map/sthlm/t-11-bana.html
Now maps show only the path but do not show of my icons. What a mistake again in my maps? Are there any robust and simple alternative to kml?
Your KMZ file is not correct. If you zip up the t-11 directory, it works:
It currently looks like this:
[lross#JJ kmz]$ unzip -l t-11-1.kmz
Archive: t-11-1.kmz
Length Date Time Name
-------- ---- ---- ----
0 07-07-13 12:06 t-11/
3655 07-07-13 12:43 t-11/t-11-bana.kml
0 07-07-13 12:08 t-11/files/
1039 07-04-13 21:21 t-11/files/subway-blue.png
If you create it from the t-11 directory so it looks like this, it works:
[lross#JJ t-11]$ unzip -l t-11-1a.kmz
Archive: t-11-1a.kmz
Length Date Time Name
-------- ---- ---- ----
0 07-07-13 12:08 files/
3655 07-07-13 12:43 t-11-bana.kml
1039 07-04-13 21:21 files/subway-blue.png
http://www.geocodezip.com/v3_GoogleEx_layer-kml_linktoB.html?filename=http://www.geocodezip.com/geoxml3_test/kmz/t-11-1a.kmz

Resources