- Home /
Decompress a file with code.
I literally tried everything. I'm a beginner so I don't know stuff like that. I know there is a documentation about GZipStream etc. and I read it, really wanted to understand it and even I used the code but I could not maybe understand it or it does not work, I guess the first option works out here. I also tried 2 of the GitHub projects. Nothing worked. I tried Zip and Unzip and Unity Archiver. Maybe I am doing something wrong but I really need to do it. I only want to decompress some new files and delete the old ones. Something like an updater system for my game. I'm downloading files from FTP server, then I want to decompress them and delete the old ones. I just need the decompression to be done. Please help. I really care about that.
Answer by xxmariofer · Jan 14, 2019 at 08:54 PM
There are a lot of assets in the asset store for extracting zip, i would suggest using simple Zip
https://assetstore.unity.com/packages/tools/simple-zip-81871
what kind of error or problems are you having? i once had a similar issue and was trying to decompress the file before making sure the file was 100% downloaded. If you give me extra info like error or code i can maybe give you a better approach
Answer by Aspect13 · Jan 14, 2019 at 09:36 PM
I can't add comment to your reply for whatever reason :/ Posting it as an answer also.
The asset can only decompress text so it won't work. Well, in the Unity Archiver on GitHub was broken script I think, couldn't fix it. In the Zip and Unzip also on GitHub. I couldn't figure out how to use it to be honest. The other errors are with GZipStream, like the name of this and this is wrong or something does not contain something, I added some namespaces but it was showing forever. Also my code for downloading is from a guy from Unity Forum. And by the way, I printed the progress of the download script and it was always -1. Here's the code:
private byte[] downloadWithFTP(string ftpUrl, string savePath = "", string userName = "", string password = "")
{
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(new Uri(ftpUrl));
//request.Proxy = null;
request.UsePassive = true;
request.UseBinary = true;
request.KeepAlive = true;
//If username or password is NOT null then use Credential
if (!string.IsNullOrEmpty(userName) && !string.IsNullOrEmpty(password))
{
request.Credentials = new NetworkCredential(userName, password);
}
request.Method = WebRequestMethods.Ftp.DownloadFile;
//If savePath is NOT null, we want to save the file to path
//If path is null, we just want to return the file as array
if (!string.IsNullOrEmpty(savePath))
{
downloadAndSave(request.GetResponse(), savePath);
return null;
}
else
{
return downloadAsbyteArray(request.GetResponse());
}
}
byte[] downloadAsbyteArray(WebResponse request)
{
using (Stream input = request.GetResponseStream())
{
byte[] buffer = new byte[16 * 1024];
using (MemoryStream ms = new MemoryStream())
{
int read;
while (input.CanRead && (read = input.Read(buffer, 0, buffer.Length)) > 0)
{
ms.Write(buffer, 0, read);
}
return ms.ToArray();
}
}
}
void downloadAndSave(WebResponse request, string savePath)
{
Stream reader = request.GetResponseStream();
//Create Directory if it does not exist
if (!Directory.Exists(Path.GetDirectoryName(savePath)))
{
Directory.CreateDirectory(Path.GetDirectoryName(savePath));
}
FileStream fileStream = new FileStream(savePath, FileMode.Create);
int bytesRead = 0;
byte[] buffer = new byte[2048];
while (true)
{
bytesRead = reader.Read(buffer, 0, buffer.Length);
if (bytesRead == 0)
break;
fileStream.Write(buffer, 0, bytesRead);
}
fileStream.Close();
}
and this is how I use it:
string path = Path.Combine(Application.persistentDataPath, "FTP Files");
path = Path.Combine(path, "data.png");
downloadWithFTP("ftp://yourUrl.com/yourFile", path, "UserName", "Password");
The original thread is here: https://stackoverflow.com/questions/43411525/download-files-via-ftp-on-android
Hello @Aspect13 i can share some code if you need it if the problem persist but use this asset, was the one i used and workds easy and fine.
https://forum.unity.com/threads/unizip-free-zipper-unzipper.220589/
I used it already. Was not working though. I will try again and see if it works and if no what errors it gives.
Then use the WWW class, it was deprecated but solved me errors when unity api couldnt. your code looks fine sorry i cant help you more.
Your answer
Follow this Question
Related Questions
Keep file structure on build. 1 Answer
Unity 4096*4096 Texture Compression 1 Answer
Problems with compression of images for ios 0 Answers
Writing to a file which i just created. 1 Answer
Decoding a 16 bit 1 channel png 0 Answers