We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

How to test an archive is password protected?

Hi, Support:
Is there any simple method to test an rar/zip/7z…is password protected?
I use the following method to know whether an archive is password protected like this:
Dim Stream As IO.Stream = ArchiveEntry.Open()
if Stream is nothing then ArchiveIsProtected =true.
Is this method a simple and right one?

hope to get your suggestion!
Thanks!

Another issue is that:
How to display the compressing progress information when compresses a big file such as finished ratio,compressed size,left size,time elapse,time left, compressing speed,compress ratio…

Hello.
To detect whether zip archive encrypted or not try to cast its entry to ArchiveEntryEncrypted:

using (Archive archive = new Archive("a.zip")){
  if (archive.Entries[0] is Aspose.Zip.ArchiveEntryEncrypted){
   // The archive is password-protected
 }
}

To detect whether rar archive encrypted or not try to cast its entry to RarArchiveEntryEncrypted:

using (RarArchive archive = new RarArchive("a.rar")){
  if (archive.Entries[0] is Aspose.Zip.Rar.RarArchiveEntryEncrypted){
   // The archive is password-protected
 }
}

To detect whether 7z archive encrypted or not try to cast its entry to SevenZipArchiveEntryEncrypted:

using (SevenZipArchive archive = new SevenZipArchive("a.7z")){
  if (archive.Entries[0] is Aspose.Zip.SevenZip.SevenZipArchiveEntryEncrypted){
   // The archive is password-protected
 }
}

For reporting progress of compression please look at this sample and CompressionProgressed event.

Thanks!
I have tried it, and I can succeed to test whether an archive is password protected.
But, I failed to convert the c demo for getting the compressionProgresssed to Vb.net one. Would you please provide me a full vb.net version of the demo?
And else, how about to get the uncompressing progress information such as finished ratio, time elapse, time left, uncompressing speed…for Vb.net demo? as well as how to test whether the password is a correct one for the password-protected archive?

Sorry, I can not provide the VB demo.
Here are some ideas how you can report progress information based on the above sample.

At any moment, finished ratio for the entry is ProceededBytes/(entry size).
The best way to measure elapsed time is to use StopWatch.
Time left can’t be calculated, you only can estimate it basing on elapsed time. So, it took (elapsed time) to compress (proceeded bytes), and the (entry size - proceeded bytes) are left. Assuming speed remains the same, you can compose the proportion (proceeded bytes)/(elapsed time) = (entry size - proceeded bytes)/(x), and so find time left estimate.
Uncompressing speed you can calculate with ProceededBytes of ExtractionProgressed event handler.

On wrong password: catch the InvalidDataException with the message "Wrong password supplied". Catching such exception indicates that supplied password was incorrect.

Thanks.
Let me to have a try.
As for how to check whether the supplied password is correct, I use this method that can reach it.
Dim stream As Stream
stream = Entry.Open(Password)
Dim Buffer(500) As Byte
stream.Read(Buffer, 1, 500)
for i as integer=0 to 499
if buffer(i)<>0 then return true
next

the following method could check whether the password is correct for most of cases,however, if the entry size is too large, this will throw wrong report.
Dim stream As Stream
stream = Entry.Open(Password)
Dim Buffer(500) As Byte
stream.Read(Buffer, 1, 500)
for i as integer=0 to 499
if buffer(i)<>0 then return true
next

I try another method like this:
Dim Zip As New Aspose.Zip.Rar.RarArchive(InFile)
Dim Entry As Aspose.Zip.Rar.RarArchiveEntryEncrypted = Zip.Entries(0)
On Error Resume Next
Dim a As String
Entry.Extract(OutFile)
a = Err.Number & " " & Err.Description
'Here a=5 Password for decryption was not supplied.

I try another method like this:
Dim Zip As New Aspose.Zip.Rar.RarArchive(InFile)
Dim Entry As Aspose.Zip.Rar.RarArchiveEntryEncrypted = Zip.Entries(0)
On Error Resume Next
Dim a As String
Entry.Extract(OutFile,Password)
a = Err.Number & " " & Err.Description
'Here a=0, however the password is a wrong one.

Could please tell me how to check it by InvalidDataException with the message "Wrong password supplied"?

Thanks!

C# code:

try {
  var archive = new RarArchive("a.rar", new RarArchiveLoadOptions() { 
  DecryptionPassword = "wrong" });
}
catch (InvalidDataException e) when (e.Message == "Wrong password supplied"){
  // Wrong password 
}