这是网上的一段话
tar jcv -f store.tar.bz2 /store z (gzip) 和 j (bzip2)分别是两种压缩方式,
记录一下我用到的方式
tar xvf 压缩文件名
-c: 建立压缩档案
-x:解压
-t:查看内容
-r:向压缩归档文件末尾追加文件
-u:更新原压缩包中的文件
这五个是独立的命令,压缩解压都要用到其中一个,可以和别的命令连用但只能用其中一个。下面的参数是根据需要在压缩或解压档案时可选的。
-z:有gzip属性的
-j:有bz2属性的
-Z:有compress属性的
-v:显示所有过程
-O:将文件解开到标准输出
下面的参数-f是必须的
-f: 使用档案名字,切记,这个参数是最后一个参数,后面只能接档案名。
# tar -cf all.tar *.jpg
这条命令是将所有.jpg的文件打成一个名为all.tar的包。-c是表示产生新的包,-f指定包的文件名。
# tar -rf all.tar *.gif
这条命令是将所有.gif的文件增加到all.tar的包里面去。-r是表示增加文件的意思。
# tar -uf all.tar logo.gif
这条命令是更新原来tar包all.tar中logo.gif文件,-u是表示更新文件的意思。
# tar -tf all.tar
这条命令是列出all.tar包中所有文件,-t是列出文件的意思
# tar -xf all.tar
这条命令是解出all.tar包中所有文件,-t是解开的意思
压缩
tar -cvf jpg.tar *.jpg //将目录里所有jpg文件打包成tar.jpg
tar -czf jpg.tar.gz *.jpg //将目录里所有jpg文件打包成jpg.tar后,并且将其用gzip压缩,生成一个gzip压缩过的包,命名为jpg.tar.gz
tar -cjf jpg.tar.bz2 *.jpg //将目录里所有jpg文件打包成jpg.tar后,并且将其用bzip2压缩,生成一个bzip2压缩过的包,命名为jpg.tar.bz2
tar -cZf jpg.tar.Z *.jpg //将目录里所有jpg文件打包成jpg.tar后,并且将其用compress压缩,生成一个umcompress压缩过的包,命名为jpg.tar.Z
rar a jpg.rar *.jpg //rar格式的压缩,需要先下载rar for linux
zip jpg.zip *.jpg //zip格式的压缩,需要先下载zip for linux
解压
tar -xvf file.tar //解压 tar包
tar -xzvf file.tar.gz //解压tar.gz
tar -xjvf file.tar.bz2 //解压 tar.bz2
tar -xZvf file.tar.Z //解压tar.Z
unrar e file.rar //解压rar
unzip file.zip //解压zip
总结
1、*.tar 用 tar -xvf 解压
2、*.gz 用 gzip -d 或者gunzip 解压
3、*.tar.gz和*.tgz 用 tar -xzf 解压
4、*.bz2 用 bzip2 -d或者用bunzip2 解压
5、*.tar.bz2用tar -xjf 解压
6、*.Z 用 uncompress 解压
7、*.tar.Z 用tar -xZf 解压
8、*.rar 用 unrar e解压
9、*.zip 用 unzip 解压
这条命令会把文件解压到当前目录下,也可以用 -C 指定解压到那个目录
busybox tar -cf xxx/xxx/abc".tar" "data/data/xxx" 压缩 data/data 数据到 xxx/xxx 目录下
busybox tar -xf xxx/xxx/abc".tar" -C "xxx/xxx" 解压并 abc.tar 并且拷贝到 xxx/xxx 目录下
unzip
busybox unzip xxx/xxx/abc".tar" -d "xxx/xxx" 解压 zip包 xxx.tar 数据到 xxx/xxx目录下
Java
1. java自带方式 zip -- 有些项目用这个还比较多
压缩
public static boolean compression(File srcFile, File thirdAppBackupFolder, File targetFile) {
List<File> allFiles = getAllFiles(srcFile, false);
if(allFiles.isEmpty()) {
Log.w(TAG, "don't have file to compression");
return true;
}
boolean ret = true;
InputStream in = null;
ZipOutputStream zipOut = null;
try {
zipOut = new ZipOutputStream(new FileOutputStream(targetFile));
for(File f : allFiles) {
int index = thirdAppBackupFolder.getPath().length() + 1;
if(index < 0) {
continue;
}
zipOut.putNextEntry(new ZipEntry(f.getPath().substring(index)));
try {
in = new FileInputStream(f);
int len;
byte[] content = new byte[READ_BYTES];
while ((len = in.read(content)) != -1 ) {
zipOut.write(content, 0, len);
}
zipOut.flush();
} catch (IOException e) {
Log.e(TAG, "compression, in.close(); have exception", e);
ret = false;
} finally {
if(in != null) {
in.close();
in = null;
}
}
}
} catch (IOException e) {
Log.d(TAG, "compression srcFile: " + srcFile + " to " + targetFile + " have exception" , e);
ret = false;
} finally {
try {
if(zipOut != null)
zipOut.close();
if(in != null)
in.close();
} catch (IOException e) {
Log.e(TAG, "decompression, close stream have exception", e);
ret = false;
}
}
return ret;
}
解压
public static boolean uncompression(File srcFile, File targetFolder) {
if(!targetFolder.exists() && !targetFolder.mkdirs()) {
Log.e(TAG, "targetFolder " + targetFolder + " is not exist, and create failed");
return false;
} else if(!targetFolder.isDirectory()) {
Log.e(TAG, "targetFolder " + targetFolder + " is not a folder");
return false;
} else if(!srcFile.exists()){
Log.e(TAG, "zip file: " + srcFile + " is not exist");
return false;
}
boolean ret = true;
ZipInputStream zipIn = null;
OutputStream out = null;
InputStream in = null;
ZipFile zipFile = null;
try {
zipFile = new ZipFile(srcFile);
zipIn = new ZipInputStream(new FileInputStream(srcFile));
ZipEntry zipEntry = null;
while((zipEntry = zipIn.getNextEntry()) != null) {
File tmp = new File(targetFolder, zipEntry.getName());
Log.d(TAG, "decompression file: " + tmp);
if (!tmp.getParentFile().exists()) {
tmp.getParentFile().mkdirs();
}
out = new FileOutputStream(tmp);
in = zipFile.getInputStream(zipEntry);
byte[] content = new byte[READ_BYTES];
int len;
while ((len=in.read(content)) != -1 ) {
out.write(content, 0, len);
}
out.flush();
}
} catch (IOException e) {
Log.e(TAG, "decompression srcFile: " + srcFile + " to " + targetFolder + " have exception", e);
ret = false;
} finally {
try {
if(zipIn != null)
zipIn.close();
if(out != null)
out.close();
if(in != null)
in.close();
if(zipFile != null)
zipFile.close();
} catch (IOException e) {
Log.e(TAG, "decompression, close stream have exception", e);
ret = false;
}
}
return ret;
2.jtar
public class Compress {
public void toCompress() {
FileOutputStream dest;
TarOutputStream out;
try {
dest = new FileOutputStream("c:/test/test.tar");
out = new TarOutputStream(new BufferedOutputStream(dest));
File[] filesToTar = new File[2];
filesToTar[0] = new File("c:/test/myfile1.txt");
filesToTar[1] = new File("c:/test/myfile2.txt");
for (File f : filesToTar) {
out.putNextEntry(new TarEntry(f, f.getName()));
BufferedInputStream origin = new BufferedInputStream(new FileInputStream(f));
int count;
byte data[] = new byte[2048];
while ((count = origin.read(data)) != -1) {
out.write(data, 0, count);
}
out.flush();
origin.close();
}
out.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String args[]){
new Compress().toCompress();
}
}
3. 其他依赖包 压缩的方式,很多,后面慢慢添加
4. .zip 和 .rar 是Windows下常用的压缩文件,在Ubuntu中如何解压?
1. unzip
sudo apt-get install unzip
unzip ./FileName.zip
2.unrar
sudo apt-get install unrar(sudo apt-get remove unrar)
unrar x ./FileName.rar
e 解压文件到当前目录
l[t,b] 列出压缩文档信息[technical, bare]
p 打印文件到标准输出
t 测试压缩我俄当
v[t,b] 列出压缩文档的详细信息[technical,bare]
x 解压文件到完整路径