Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to achieve 7z format compression?
#1
I often use 7z format for compressed files, I found in the QM function zip function, but can not be used for 7z format :oops:

how to write function to achieve 7z file compression and decompression? Any comments and suggestions are welcome, thank you very much Smile

On the Internet I find some information, I found someone using sevenzipsharp dll file, achieved in the CS code to achieve the pressure and decompression, I programming level is not good, I hope QM developers can help me achieve this function, this feature Is also useful for most people, the compression rate of the normal zip file format is too low Smile

SevenZipSharp.dll file is very small, only 147k and 7z official installation package inside the dll file is very large, there are more than 1M, I hope to achieve the call SevenZipSharp.dll compression and decompression, This is not necessary to install 7z compression software, very good Idea Idea

http://sevenzipsharp.codeplex.com/


Attached Files
.dll   SevenZipSharp.dll (Size: 147.5 KB / Downloads: 44)
#2
Use 7z to achieve file compression and decompression of the code Idea

/**
* <p><b>Copyright: All Rights Reserved © 2015-2016</b></p>
* <p><b>Company:Travelsky</b></p>
* @ClassName: Zip7ZUtil
* @date 2015-5-14 2:06:25
*/
public class Zip7zUtil {
private static String exePath=SysPropUtil.getValue("7z.path");
private static Logger logger = LogManager.getLogger(Zip7zUtil.class);
public static void zip(String srcRootPath,String zipFile) {
String cmd = "7z a -tzip " + zipFile + " " + srcRootPath;
try {
Runtime.getRuntime().exec(cmd);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

/**
* @param zipFilePath
* @param unZipPath
*/
/*public static boolean syncUnzip(String zipFilePath,String unZipPath) {
logger.info("unzip "+zipFilePath+" to "+unZipPath);
boolean result;
String cmd = exePath+"\\7z x -o" + unZipPath + " -y " + zipFilePath;
logger.info("do syncUnzip exec : "+cmd);
try {
Process p = Runtime.getRuntime().exec(cmd);
BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line = null;
while (true) {
try {
line = br.readLine();
if (line == null) {
result = true;
break;
}else{
System.out.println(line);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
logger.error(" syncUnzip exception : "+e);
result = false;
}

}

} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
logger.error(" syncUnzip exception : "+e);
result = false;
}
return result;
}*/

public static boolean syncUnzip(String zipFilePath,String unZipPath) {
logger.info("unzip "+zipFilePath+" to "+unZipPath);
boolean result;
String cmd = exePath+"\\7z x -o" + unZipPath + " -y " + zipFilePath;
logger.info("do syncUnzip exec : "+cmd);
try {
File zipFile=new File(zipFilePath);
long zipSize=zipFile.length();
long unzipSize=0;
Process p = Runtime.getRuntime().exec(cmd);
BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line = null;
while (true) {
try {
line = br.readLine();
logger.info("7z info : " + line);
if (line != null) {

String[] arr=line.split("=");
if (arr.length==2) {
if (arr[0].trim().equals("Physical Size")) {
zipSize=Long.parseLong(arr[1].trim());
}
}
String[] arr2=line.split(":");
if (arr2[0].trim().equals("Compressed")) {
unzipSize=Long.parseLong(arr2[1].trim());
}
}else{
logger.info("unzipSize = "+unzipSize+" , zipSize = "+zipSize);
if (unzipSize==zipSize) {
result = true;
}else{
result = false;
logger.error(" syncUnzip "+zipFilePath+" to "+unZipPath+" failed ! ");
}
break;
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
logger.error(" syncUnzip exception : "+e);
result = false;
}

}

} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
logger.error(" syncUnzip exception : "+e);
result = false;
}
return result;
}
/*
* @param zipFilePath
* @param unZipPath
* @param listener
*/
public static void asyncUnzip(String zipFilePath,String unZipPath,final ICallbackListener listener) {
if (zipFilePath == null || zipFilePath.equals("") || unZipPath == null || unZipPath.equals("")) {
listener.callback("fail");
return;
}
String cmd = exePath+"\\7z x -o" + unZipPath + " -y -tzip " + zipFilePath;
try {
final Process p = Runtime.getRuntime().exec(cmd);
if (listener != null) {
new Thread() {
public void run() {
BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line = null;
while (true) {
try {
line = br.readLine().trim();
if (line == null) {
listener.callback("OK");
break;
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
listener.callback("fail");
}

}
}
};
}


} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
listener.callback("fail");
}
}
}
#3
Macro Compress 7z (7-Zip)
Code:
Copy      Help
str sevenZip.expandpath("$qm$\7z.exe") ;;from c:\program files\7-zip\
;RunConsole2(F"''{sevenZip}'' -?"); ret

str file1="$qm$\qm.exe"
str file2="$my qm$\qm.7z"
file1.expandpath
file2.expandpath
out RunConsole2(F"''{sevenZip}'' a ''{file2}'' ''{file1}''")

;qm.exe - 1447 KB
;qm.zip - 791 KB
;qm.7z - 644 KB
#4
Thank you very much, I use the above code, press the file successfully! Smile

I would like to know:

1. how to set the password of the compressed file?

2.How do I extract a compressed file with a password?

I use the following command line to unzip the compressed file with the password(filepassword),-p is the encrypted parameter:

7z.exe e file.7z -pfilepassword

3.How to encrypt the password (do not directly display the password) Otherwise, through some binary view tool, can view my password :oops:
#5
I use the following code to extract the file, the result is failed, the hint message is behind, where is the wrong? Confusedhock:


Macro Macro2
Code:
Copy      Help
str sevenZip.expandpath("$qm$\7z.exe")
str file="$desktop$\test"
str zfile="$desktop$\test\file.7z"
file.expandpath
zfile.expandpath
RunConsole2(F"''{sevenZip}'' e ''{zfile}'' ''{file}''")

---------------------------------------------------------------------------------------------------------------------------------------


7-Zip [32] 16.04 : Copyright © 1999-2016 Igor Pavlov : 2016-10-04

Scanning the drive for archives:
1 file, 87527 bytes (86 KiB)

Extracting archive: C:\Users\Administrator\Desktop\test\file.7z
--
Path = C:\Users\Administrator\Desktop\test\file.7z
Type = 7z
Physical Size = 87527
Headers Size = 122
Method = LZMA2:384k
Solid = -
Blocks = 1


No files to process
Everything is Ok

Files: 0
Size: 0
Compressed: 87527
#6
This is the success of the code: Smile

Macro Macro2
Code:
Copy      Help
str z7.expandpath("$qm$\7z.exe")
str zf.expandpath("$desktop$\test\file.7z")
str f.expandpath("$desktop$\test\file.doc")
RunConsole2(F"''{z7}'' a ''{zf}'' ''{f}''")

This is the code that failed :?:

Macro Macro3
Code:
Copy      Help
str z7.expandpath("$qm$\7z.exe")
str zf.expandpath("$desktop$\test\file.7z")
str f.expandpath("$desktop$\test\")
RunConsole2(F"''{z7}'' e ''{zf}'' ''{f}''")
#7
I google search results, see someone with 7z.dll a file, the definition of the function of calling dll, achieved in the Cs code to achieve the compression of the file is also decompression, I think in QM should also be able to achieve, Than the above code should be good, The above code requires two files, 7z.exe and 7z.dll! how to write a function? How to call? Smile
#8
Gintaras Wrote:Macro Compress 7z (7-Zip)
Code:
Copy      Help
str sevenZip.expandpath("$qm$\7z.exe") ;;from c:\program files\7-zip\
;RunConsole2(F"''{sevenZip}'' -?"); ret

str file1="$qm$\qm.exe"
str file2="$my qm$\qm.7z"
file1.expandpath
file2.expandpath
out RunConsole2(F"''{sevenZip}'' a ''{file2}'' ''{file1}''")

;qm.exe - 1447 KB
;qm.zip - 791 KB
;qm.7z - 644 KB

I streamlined the code and used the following code to compress the file,I would like to ask, in the Qm software, all the variables on both sides of the quotation marks, I can delete?

Macro Macro2
Code:
Copy      Help
str z7.expandpath("$qm$\7z.exe")
str zf.expandpath("$desktop$\test\file.7z")
str f.expandpath("$desktop$\test\file.doc")
RunConsole2(F"{z7} a {zf} {f}")
#9
I use the following code, can not extract the file.7z file to the test folder, hope someone can help me, thanks in advance :oops:

Macro Macro2
Code:
Copy      Help
str z7.expandpath("$qm$\7z.exe")
str zf.expandpath("$desktop$\test\file.7z")
str f.expandpath("$desktop$\test\")
RunConsole2(F"{z7} e {zf} {f}")
#10
This might be of some help:
Quick Macros and Peazip (auto generate complex command-line)
#11
Thank you very much that you are doing great,

I would like to ask, if the command line in the compression (or decompression) password, how to protect in the QM :oops:
#12
If you can write a function with a dialog box to set up more convenient, I am not very good programming, it is a bit difficult to operate

Thank you for your technical support
#13
I found the easiest code to compress and decompress using 7z dll in Google. I hope someone can convert C# code into QM code, thanks in advance. Smile

Compression and decompression using 7Z in C#

1. Add a reference to the solution: SevenZipSharp.dll
2. Copy and paste two dll files: 7z.dll 7z64.dll and modify the attribute "copy to output directory" to "if
New copy"

Code:
Copy      Help
//Test compression
           var zip = new SevenZipCompressor();
           zip.CompressionLevel=CompressionLevel.High;
           zip.CompressDirectory(@"c:\doc", @"c:\arch.7z");

           //Test decompression
           using (var tmp = new SevenZipExtractor(@"c:\arch.7z"))
           {
               for (var i = 0; i < tmp.ArchiveFileData.Count; i++)
               {
                   tmp.ExtractFiles(@"C:\cygwin\", tmp.ArchiveFileData[i].Index);
               }
           }
#14
If you can find 7z.dll documentation or header file, you can use it in QM directly without C#. But need to know how to convert C dll declarations to QM.
If you want to use SevenZipSharp.dll in QM, start from menu File / New / Templates / Scripting / C#...

I cannot help much. You can use the command line program, it is easy.
#15
https://www.7-zip.org/sdk.html

dowload and read, may be useful
#16
If you need to quickly generate specific command-lines for 7z.
For example:
- compress with a certain compression level
- compress with a certain compression level and set password
- single volume or split
-...etc...

Use peazip!
Peazip uses 7zip to pack/unpack 7z supported archives except peazips own archive format.
Within peazip you can setup how you want pack a file and then go to the "console" tab, a command-line will be generated based on the parameters you provided (compression, password,....). All the command-lines are for use with7z.exe except if you chose the pea format.

More info here:
http://www.quickmacros.com/forum/showthr...p?tid=6161


Forum Jump:


Users browsing this thread: 1 Guest(s)