iOS 压缩和解压

iOS开发 - 文件压缩与解压缩 第三方解压缩框架——SSZipArchive下载地址:https://github.com/samsoffes/ssziparchive 注意:需要引入libz.dylib框架 // Unzipping NSString *zipPath = @"path_to_your_zip_file"; NSString *destinationPath =@"path_to_the_folder_wher 阅读详情

一、完成文件下载

需求:完成文件下载

1.在本地服务器中,添加一个图片的压缩文件。

2.代码示例:

文件下载器代码:

头文件

复制代码
 1 //
 2 //  YYfileDownloader.h
 3 //  01-文件的下载(不合理)
 4 //
 5 //  Created by apple on 14-7-1.
 6 //  Copyright (c) 2014年 itcase. All rights reserved.
 7 //
 8 
 9 #import <Foundation/Foundation.h>
10 
11 @interface YYfileDownloader : NSObject
12 //下载的远程url(连接到服务器的路径)
13 @property(nonatomic,strong)NSString *url;
14 //下载后的存储路径(文件下载到什么地方)
15 @property(nonatomic,strong)NSString *destPath;
16 //是否正在下载(只有下载器内部清楚)
17 @property(nonatomic,readonly,getter = isDownloading)BOOL Downloading;
18 //用来监听下载进度
19 @property(nonatomic,copy)void (^progressHandler)(double progress);
20 //用来监听下载完成
21 @property(nonatomic,copy)void (^completionHandler)();
22 //用来监听下载错误
23 @property(nonatomic,copy)void(^failureHandler)(NSError *error);
24 -(void)pause;
25 -(void)start;
26 @end
复制代码

实现代码:

复制代码
  1 //
  2 //  YYfileDownloader.m
  3 //  01-文件的下载(不合理)
  4 //
  5 //  Created by apple on 14-7-1.
  6 //  Copyright (c) 2014年 itcase. All rights reserved.
  7 //
  8 
  9 #import "YYfileDownloader.h"
 10 
 11 @interface YYfileDownloader ()<NSURLConnectionDataDelegate>
 12 //请求对象
 13 @property(nonatomic,strong)NSURLConnection *cnnt;
 14 //文件句柄
 15 @property(nonatomic,strong)NSFileHandle *writeHandle;
 16 //当前获取到的数据长度
 17 @property(nonatomic,assign)long long currentLength;
 18 //完整数据长度
 19 @property(nonatomic,assign)long long sumLength;
 20 
 21 @end
 22 @implementation YYfileDownloader
 23 //开始下载
 24 -(void)start
 25 {
 26         _Downloading=YES;
 27     
 28     //创建一个请求
 29     NSURL *URL=[NSURL URLWithString:self.url];
 30     NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:URL];
 31     
 32     //设置请求头信息
 33     //self.currentLength字节部分重新开始读取
 34     NSString *value=[NSString stringWithFormat:@"bytes=%lld-",self.currentLength];
 35     [request setValue:value forHTTPHeaderField:@"Range"];
 36     
 37     //发送请求(使用代理的方式)
 38     self.cnnt=[NSURLConnection connectionWithRequest:request delegate:self];
 39 
 40 }
 41 
 42 //暂停下载
 43 -(void)pause
 44 {
 45     _Downloading=NO;
 46     //取消发送请求
 47     [self.cnnt cancel];
 48     self.cnnt=nil;
 49 }
 50 
 51 #pragma mark- NSURLConnectionDataDelegate代理方法
 52 /*
 53  *当接收到服务器的响应(连通了服务器)时会调用
 54  */
 55 -(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
 56 {
 57 #warning 判断是否是第一次连接
 58     if (self.sumLength) return;
 59     
 60     //1.创建文件存数路径
 61     NSString *caches=[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
 62     NSString *filePath=[caches stringByAppendingPathComponent:@"images.zip"];
 63     
 64     
 65     
 66     //2.创建一个空的文件,到沙盒中
 67     NSFileManager *mgr=[NSFileManager defaultManager];
 68     //刚创建完毕的大小是o字节
 69     [mgr createFileAtPath:filePath contents:nil attributes:nil];
 70     
 71     //3.创建写数据的文件句柄
 72     self.writeHandle=[NSFileHandle fileHandleForWritingAtPath:filePath];
 73     
 74     //4.获取完整的文件长度
 75     self.sumLength=response.expectedContentLength;
 76 }
 77 
 78 /*
 79  *当接收到服务器的数据时会调用(可能会被调用多次,每次只传递部分数据)
 80  */
 81 -(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
 82 {
 83     //累加接收到的数据长度
 84     self.currentLength+=data.length;
 85     //计算进度值
 86     double progress=(double)self.currentLength/self.sumLength;
 87 //    self.progress.progress=progress;
 88     if (self.progressHandler) {//传递进度值给block
 89         self.progressHandler(progress);
 90         
 91         //相当于在此处调用了下面的代码
 92 //        ^(double progress)
 93 //        {
 94 //            //把进度的值,传递到控制器中进度条,以进行显示
 95 //            vc.progress.progress=progress;
 96 //        };
 97     }
 98     
 99     
100     //一点一点接收数据。
101     NSLog(@"接收到服务器的数据!---%d",data.length);
102     //把data写入到创建的空文件中,但是不能使用writeTofile(会覆盖)
103     //移动到文件的尾部
104     [self.writeHandle seekToEndOfFile];
105     //从当前移动的位置,写入数据
106     [self.writeHandle writeData:data];
107 }
108 
109 /*
110  *当服务器的数据加载完毕时就会调用
111  */
112 -(void)connectionDidFinishLoading:(NSURLConnection *)connection
113 {
114     NSLog(@"下载完毕----%lld",self.sumLength);
115     //关闭连接,不再输入数据在文件中
116     [self.writeHandle closeFile];
117     self.writeHandle=nil;
118     
119     //清空进度值
120     self.currentLength=0;
121     self.sumLength=0;
122     
123     if (self.completionHandler) {//下载完成通知控制器
124         self.completionHandler();
125         //相当于下面的代码
126 //        ^{
127 //            NSLog(@"下载完成");
128 //            [self.btn setTitle:@"下载已经完成" forState:UIControlStateNormal];
129 //        }
130     }
131 
132 }
133 /*
134  *请求错误(失败)的时候调用(请求超时\断网\没有网\,一般指客户端错误)
135  */
136 -(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
137 {
138     if (self.failureHandler) {//通知控制器,下载出错
139         self.failureHandler(error);
140     //相当于调用了下面的代码
141 //        ^{
142 //            NSLog(@"下载错误!");
143 //        }
144     }
145 }
146 
147 @end
复制代码

主控制器中的控制代码:

复制代码
 1 //
 2 //  YYViewController.m
 3 //  01-文件下载(解压)
 4 //
 5 //  Created by apple on 14-7-2.
 6 //  Copyright (c) 2014年 itcase. All rights reserved.
 7 //
 8 
 9 #import "YYViewController.h"
10 #import "YYfileDownloader.h"
11 
12 @interface YYViewController ()
13 @property(nonatomic,strong)YYfileDownloader *fileDownloader;
14 //@property (weak, nonatomic) IBOutlet UIButton *btn;
15 @property (weak, nonatomic) IBOutlet UIProgressView *progress;
16 @end
17 
18 @implementation YYViewController
19 
20 - (void)viewDidLoad
21 {
22     [super viewDidLoad];
23 }
24 
25 #pragma mark-懒加载
26 -(YYfileDownloader *)fileDownloader
27 {
28     if (_fileDownloader==nil) {
29         _fileDownloader=[[YYfileDownloader alloc]init];
30         //设置文件下载路径
31         _fileDownloader.url=@"http://192.168.1.53:8080/MJServer/resources/images.zip";
32         
33         //设置文件保存路径
34         NSString *caches=[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
35         NSString *filePath=[caches stringByAppendingPathComponent:@"images.zip"];
36         _fileDownloader.destPath=filePath;
37         
38         _fileDownloader.progressHandler=^(double progress)
39         {
40             NSLog(@"下载进度-----%f",progress);
41         };
42         _fileDownloader.completionHandler=^{
43             NSLog(@"下载完成");
44             //下载完成后,对文件进行解压
45            
46         };
47         _fileDownloader.failureHandler=^(NSError *error){
48             NSLog(@"下载错误!%@",error);
49         };
50     }
51     return _fileDownloader;
52 }
53 
54 //当手指触摸屏幕的时候,下载压缩文件
55 -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
56 {
57     [self.fileDownloader start];
58 }
59 
60 
61 @end
复制代码

3.打印查看:

在沙盒中,查看下载到的数据。

二、实现解压

需求:下载完成后,对文件实现解压

说明:这里使用一个纯C语言的库进行解压。

1.第三方解压框架介绍:

第三方解压缩框架——SSZipArchive

下载地址:https://github.com/samsoffes/ssziparchive

注意:需要引入libz.dylib框架

(1)解压

NSString *zipPath = @"path_to_your_zip_file";

NSString *destinationPath = @"path_to_the_folder_where_you_want_it_unzipped";

[SSZipArchive unzipFileAtPath:zipPath toDestination:destinationPath];

 

(2) 压缩

NSString *zippedPath = @"path_where_you_want_the_file_created";

NSArray *inputPaths = [NSArray arrayWithObjects:

       [[NSBundle mainBundle] pathForResource:@"photo1" ofType:@"jpg"],

         [[NSBundle mainBundle] pathForResource:@"photo2" ofType:@"jpg"]nil];

[SSZipArchive createZipFileAtPath:zippedPath withFilesAtPaths:inputPaths];

(3)图示

2.使用:

(1)导入第三方框架 

  

(2)导入头文件

(3)点击直接运行会报错。原因是它缺乏一个库,这个框架的使用依赖于libz.dylib这个库,需要导入这个库才能使用。

    

(4)运行程序,代码示例

 

解压功能实现代码如下:

复制代码
 1 //
 2 //  YYViewController.m
 3 //  01-文件下载(解压)
 4 //
 5 //  Created by apple on 14-7-2.
 6 //  Copyright (c) 2014年 itcase. All rights reserved.
 7 //
 8 
 9 #import "YYViewController.h"
10 #import "YYfileDownloader.h"
11 #import "SSZipArchive.h"
12 
13 @interface YYViewController ()
14 @property(nonatomic,strong)YYfileDownloader *fileDownloader;
15 //@property (weak, nonatomic) IBOutlet UIButton *btn;
16 @property (weak, nonatomic) IBOutlet UIProgressView *progress;
17 @end
18 
19 @implementation YYViewController
20 
21 - (void)viewDidLoad
22 {
23     [super viewDidLoad];
24 }
25 
26 #pragma mark-懒加载
27 -(YYfileDownloader *)fileDownloader
28 {
29     if (_fileDownloader==nil) {
30         _fileDownloader=[[YYfileDownloader alloc]init];
31         //设置文件下载路径
32         _fileDownloader.url=@"http://192.168.1.53:8080/MJServer/resources/images.zip";
33         
34         //设置文件保存路径
35         NSString *caches=[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
36         NSString *filePath=[caches stringByAppendingPathComponent:@"images.zip"];
37         _fileDownloader.destPath=filePath;
38         
39         _fileDownloader.progressHandler=^(double progress)
40         {
41             NSLog(@"下载进度-----%f",progress);
42         };
43         _fileDownloader.completionHandler=^{
44             NSLog(@"下载完成");
45             //下载完成后,对文件进行解压
46            //使用第三方框架,对下载的压缩文件进行解压
47             //注意:如果文件较大的话,解压过程会比较耗时,所以最好把它放到异步线程去执行
48         dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
49             //参数说明:第一个参数为要解压的文件路径,第二个参数为解压后的文件保存路径
50             [SSZipArchive unzipFileAtPath:filePath toDestination:caches];
51             NSLog(@"-----%@",[NSThread currentThread]);
52             NSLog(@"已经解压完成!");
53         });
54        
55         };
56         _fileDownloader.failureHandler=^(NSError *error){
57             NSLog(@"下载错误!%@",error);
58         };
59     }
60     return _fileDownloader;
61 }
62 
63 //当手指触摸屏幕的时候,下载压缩文件
64 -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
65 {
66     [self.fileDownloader start];
67 }
68 
69 
70 @end
复制代码

查看沙盒:

打印查看:

 

三、压缩文件

需求:把图片压缩后,写入到沙盒缓存中

1.向项目中添加图片,以备演示

2.示例代码

复制代码
 1 //
 2 //  YYViewController.m
 3 //  01-文件下载(解压)
 4 //
 5 //  Created by apple on 14-7-2.
 6 //  Copyright (c) 2014年 itcase. All rights reserved.
 7 //
 8 
 9 #import "YYViewController.h"
10 #import "YYfileDownloader.h"
11 #import "SSZipArchive.h"
12 
13 @interface YYViewController ()
14 @property(nonatomic,strong)YYfileDownloader *fileDownloader;
15 //@property (weak, nonatomic) IBOutlet UIButton *btn;
16 @property (weak, nonatomic) IBOutlet UIProgressView *progress;
17 @end
18 
19 @implementation YYViewController
20 
21 //当手指触摸屏幕的时候,对文件进行压缩
22 -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
23 {
24 //    [self.fileDownloader start];
25 //    [[NSBundle mainBundle]pathForResource:@"minion_01.png"  ofType:nil];
26 //    [[NSBundle mainBundle]pathForResource:@"minion_01.png"  ofType:nil];
27     
28     //1.获取mainBundle中的所有png图片
29     NSArray *pngs=[[NSBundle mainBundle]pathsForResourcesOfType:@"png" inDirectory:nil];
30     
31     NSString *caches=[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
32     NSString *filePath=[caches stringByAppendingPathComponent:@"img.zip"];
33     //第一个参数为压缩后的文件路径,第二个参数为压缩的文件数组
34     [SSZipArchive createZipFileAtPath:filePath withFilesAtPaths:pngs];
35     
36 }
37 
38 
39 @end
复制代码

查看沙盒:

注意:路径一定能要是全路径。

iOS应用中创建和解压加密ZIP文件的完整教程.zip ZIP是一种广泛使用的压缩文件格式,它通过减少文件大小以节省存储空间,或在传输过程中减少所需带宽。ZIP文件中可以包含多个文件和文件夹,并且通常采用无损压缩算法。这种格式在跨平台间共享文件时非常实用,支持的操作系统包括Windows、macOS、Linux等。CocoaPods 是一个用于 iOS 和 macOS 项目的依赖管理器,它能够帮助开发者轻松地管理项目所依赖的第三方库。在本章节中,我们将详细介绍如何安装和配置 CocoaPods 以使用 SSZipArchive 库。 阅读详情

相关推荐

ZIPFoundation:在Swift中轻松进行ZIP处理

ZIP Foundation是一个用于创建,读取和修改ZIP存档文件的库。 它是用Swift编写的,基于来实现高性能和高能效。 要了解有关框架性能特征的更多信息,可以阅读。 产品特点 现代Swift API 高性能压缩和解压缩 确定性内存消耗 Linux兼容性 没有第三方依赖关系(在Apple平台上,在Linux上为zlib) 全面的单元和性能测试范围 完整的文件 要求 iOS 9.0+ / macOS 10.11+ / tvOS 9.0+ / watchOS 2.0+ 或带有zlib开发包Linux Xcode 10.0 迅捷4.0 安装 迅捷软件包管理器 Swift P

xcode安装压缩解压工具ZIPFoundation

ZIPFoundation用于压缩解压参考地址。

m0_37660341的博客 607

【CUT&TAG分析-加了spike in的样品】

使用 Bowtie2 或 Subread (subjunc) 来构建大肠杆菌的索引。bowtie2比对时设置参数–local, 局部比对。

weixin_57975238的博客 1400

iOS开发网络篇—文件下载(六·压缩和解压)

iOS开发网络篇—文件下载(六·压缩和解压) 一、完成文件下载 需求:完成文件下载 1.在本地服务器中,添加一个图片的压缩文件。 2.代码示例: 文件下载器代码: 头文件 1 // 2 // YYfileDownloader.h 3 // 01-文件的下载(不合理) 4 // 5 // Created by apple on 1

huangznian的专栏 1521

别再手动算K值了!ZEMAX中两种锥透镜建模方法详解(标准面 vs. 轴锥面)

本文详细解析了ZEMAX中锥透镜的两种建模方法:标准面与轴锥面。通过对比两者的精度、计算效率和适用场景,帮助工程师在激光整形、光束匀化等应用中做出最优选择,并提供了参数设置和异常排查的实用技巧。

weixin_42548874的博客 321

Windows安装Pytorch3d

pytorch和pytorch3d版本对应关系如下:https://github.com/facebookresearch/pytorch3d/releasescuda和NVIDIA CUB版本对应关系如下:https://github.com/NVIDIA/cub/releases?page=1本人配置如下:GPU:1050TiCUDA:11.1python:3.8。

Snakewood的博客 4286

IOS开发—图片压缩/解压成Zip文件

图片压缩/解压成Zip文件 本文介绍如何将图片压缩成Zip文件,首先需要下载第三方库ZipArchive 并导入项目中。 ZipArchive 库地址:https://github.com/mattconnolly/ZipArchive 一、文档结构: 二、准备工作:   1、框架导入:          2、ZipArchive.m文件使用非ARC机制

Lotheve的私房菜 3518

Bentley二次开发教程28-交互窗口-案例实战2-EC属性赋值工具

该工具会根据用户输入的路径获取Schema文件的路径,然后通过路径读取对应的Schema文件并在树状图中显示EC类,当用户选择树状图上的节点时,此时会触发事件,根据EC属性添加用户自定义控件用于显示EC属性并可对其进行赋值操作。完成后选取元素,点击界面上的应用,此时元素会被赋予指定EC类,同时修改的EC属性值也会被同步赋予。该工具涉及到的WinForm控件有:TextBox,Button,TreeView,Panel,UserControl(用户自定义控件)。

不见不散的博客 1474

iOS gzip 压缩和解压

压缩: #import @interface LFCGzipUtility : NSObject {      } +(NSData*) gzipData: (NSData*)pUncompressedData; @end #import "zlib.h" #import "LFCGzipUtil

tianxiawoyougood的博客 7280

泛微OA E9文档管理优化:临时文件清理与存储配置指南

本文深入解析了泛微OA E9系统性能下降的常见原因——临时文件堆积,并提供了完整的优化配置指南。文章详细介绍了临时文件的产生机制,并手把手指导管理员如何通过修改存储路径、调整保留时间、设置白名单等关键配置,有效清理与管理系统临时文件,从而释放存储空间,提升系统运行效率与稳定性。

tree8的博客 431

【目标检测-YOLO】YOLOv5-v5.0-yolov5s网络架构详解(第一篇)

源码:GitHub - ultralytics/yolov5 at v5.0 由于YOLO v5 代码库在持续更新,如上图,有多个版本,每个版本的网络结构不尽相同。以下内容以 v5.0 为准,网络结构选用 yolov5s。 为了方便画图和理解网络结构,选用可视化工具:Netron 网页版进行可视化, 然后使用PPT作图。 在Releases · ultralytics/yolov5 · GitHub找到 v5.0中的Assets 中的yolov5s.pt并下载该权重。 下图为直接......

博观约取,厚积薄发 2万+

iOS压缩和解压

iOS中,有时候我们需要从服务器下载资源文件,有2种方式,有时是单个文件传输,有时服务器会将多个文件压缩后进行传输。当资源文件较多时,压缩成zip格式,通过验证文件的MD5值判断文件是否成功下载是一种不错的方式,这里讲一下客户端如何处理zip文件和压缩文件为zip包 我们项目使用的是第三方开源库SSZipArchive,首先在githHub 下载源码,导入项目,使用起来很简单,在需要用的地方

lj20082100的专栏 1503

【3天快速入门WPF】从小白到新手入门教程

嘿,小伙伴们!如果你已经有一定的C#开发基础,但想快速掌握WPF开发,达到企业级水准,那接下来的这个三天快速入门计划绝对适合你!虽然听起来有点挑战,但别担心,只要跟着这个高强度、结构化的学习计划,你一定能搞定它。首先,我们得搞清楚,WPF的核心知识点到底有哪些。要想达到企业级开发水准,XAML、数据绑定、MVVM、控件使用、命令系统、样式模板这些统统都得拿下。听起来是不是有点多?别急,我们把任务分解到三天里,每天安排合理的内容,保证理论和实践相结合,让你学得扎实又高效。

Zy100Papa的博客 1423

iOS学习笔记之文件的压缩和解压(zip)

*******************************文件的压缩和解压***************************** 运行环境:Xcode7 iOS9.0 第三方框架:SSZipArchive 动态库:libz.tbd /**  * zip : 压缩文件  * filePath : 压缩后存放的路径  * directory : 需要压缩的文件夹路

CoderKo1o 2708

TIA博途选型工具Selection Tool的基本使用方法介绍

TIA博途选型工具Selection Tool的基本使用方法介绍

Robot_PLC_HMI_Automation 4212

廖雪峰Python教程[完整版]

廖雪峰的python教程整理的,已经排好版了,而且还是完整的,适合小白

iOS 之AipArchive压缩和解压

在本教程中,我将演示如何在iOS程序中压缩和解压缩文件。我们将使用一个叫做ZipArchive的第三方库来实现。尽管压缩和解压缩有许多其他的方案可选,但是我觉得ZipArchive库是最快捷、最简单的解决方案。let’s go。 为什么我需要解压缩文件 有许多原因能解释为什么我要在工程中使用压缩和解压缩功能,下面是几个常见的原因: 苹果App Store的50M下载限制

Shicuf的专栏 512

IDEA的使用指南

IDEA基于java的使用手册,可以最快速的上手,在使用中不断的丰富自己的使用技巧.

iOS中使用ZipArchive压缩和解压文件

iOS中使用ZipArchive压缩和解压缩文件   引自 http://www.36duo.com/thread-119-1-1.html 在本教程中,我将演示如何在iOS程序中压缩和解压缩文件。我们将使用一个叫做ZipArchive的第三方库来实现。尽管压缩和解压缩有许多其他的方案可选,但是我觉得ZipArchive库是最快捷、最简单的解决方案。 为什么我需要解压缩

steve_e的博客 970

老版QQ原生表情gif动态

这是老版的QQ表情,不过全是动态的的,Windows照片查看器是不会动的,可以右击用浏览器打开查看,再说一遍是老版的,只是分享给宁愿用老版也要动态的人。

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

IOS学习之压缩和解压工具使用

文件压缩和解压方法: //ZLib压缩 + (NSData *)zlibCompressData:(NSData *)sourceData {          NSUInteger sourceDataLength = [sourceData length];          if (sourceDataLength == 0) {         return sourceDa

南北雪树的专栏 1234

iOS 压缩和解压,获取解压包内文件以及删除方法总结

最近在开发中需要进行压缩和解压的操作,网上参考了一些大神们的博客和文章,整理一下自己的思路,记录一下。 从http://code.google.com/p/ziparchive/ 上下载ZipArchive.zip,解压后将代码加入工程中,把libz库添加到工程中。然后依照惯例准备几个测试Button和几张图片。 //压缩 - (IBAction)CloseZipFile:(UIButton ...

weixin_30929011的博客 327

iOS网络-ZipArchive框架的文件压缩和解压

导入第三方框架ZipArchive之后还要在系统库文件中导入一个如下文件(搜索libz出来的任何一个都可以) 导入的头文件是#import "Main.h" 文件压缩 -(void)zip { NSArray *arrayM = @[@"/Users/gengqun/Desktop/Snip20160118_866.png", ...

weixin_34413802的博客 109

iOS:压缩和解压live图(.livp)——百度一刻相册+百度网盘识别支持

iOS:压缩和解压live图(.livp)——百度一刻相册+百度网盘识别支持 市面上首篇把静态图+mov短视频打包回live图的方案, 不需要iPhone参与

<font color="Pink">Tisfy的博客</font> 851

ZipArchive: 快速、简单易用的 iOS 和 macOS 压缩库

是一个开源的 iOS 和 macOS 库,用于快速地压缩和解压 ZIP 文件。它提供了简单的 API,使开发者能够轻松地将压缩和解压功能集成到自己的应用程序中。 ## 功能特性 ZipArchive 支持以下主要功能: - 支持在 iOS 和 macOS 上压缩和解压 ZIP 文件。 - 提供简单的 API,易于集成到现有项目中。 - 可以从文件系统或数据流中读取和写入 ZIP 文件。 - ...

gitblog_00004的博客 710

IOS使用ZipArchive压缩解压文件

在项目过程中需要对从服务器拉取的压缩文件进行解压,并且需要把解压后的文件修改后再压缩上传到服务器;调研后使用ZipArchive对文件进行压缩和解压。 下面是ZipArchive的介绍和使用: 1、ZipArchive是压缩和解压在iOS和Mac文件的一种简单实用类,具有以下功能:      a.解压zip文件      b.解压加密的zip文件      c.创建zip文件

showmyself 1264

[Swift通天遁地]七、数据与安全-(10)文件的加密压缩和解压加密压缩

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/)➤GitHub地址:https://github.com/strengthen/LeetCode➤原文地址:https://www.cnblogs.com/streng...

weixin_30758821的博客 154

用ZipArchive添加和解压zip包

ZipArchive是一个开源的zip开发包工具。 使用方法如下:   创建/添加一个zip包 ZipArchive* zipFile = [[ZipArchive alloc] init]; //次数得zipfilename需要一个完整得路径,例如***/Documents/demo.zip [zipFile CreateZipFile2:@"zipfilename"];  //有两种

u012202641的专栏 1232

IOS解压缩和压缩zip文件

使用开源库ZipArchive ZipArchive资源下载地址 解压: NSString *zipPath = @"zip压缩包目录"; if ([[NSFileManager defaultManager] fileExistsAtPath:savePath]) { ZipArchive *zip = [[ZipArchive alloc] i...

C 1367
上一篇: APP版本更新
下一篇: NSLayoutConstraint使用
JustinACE
博客等级 码龄12年 0粉丝 17原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值