Skip to content

Commit

Permalink
Added example for LZPacker::compress()
Browse files Browse the repository at this point in the history
  • Loading branch information
tobozo committed Jan 1, 2025
1 parent c788328 commit 62e69a6
Show file tree
Hide file tree
Showing 4 changed files with 15,140 additions and 0 deletions.
107 changes: 107 additions & 0 deletions examples/ESP32/Deflate_lz77/Deflate_lz77.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
#ifndef ESP32
#error "this sketch is only available on ESP32 architecture"
#endif

// Set **destination** filesystem by uncommenting one of these:
//#define DEST_FS_USES_SPIFFS
#define DEST_FS_USES_LITTLEFS
//#define DEST_FS_USES_SD
//#define DEST_FS_USES_FFAT // ESP32 only
//#define DEST_FS_USES_SD_MMC // ESP32 only
#include <ESP32-targz.h>


void setup()
{
Serial.begin(115200);
delay(1000);
Serial.println("Hello world");

if(!LittleFS.begin())
{
Serial.println("LittleFS Failed, halting");
while(1);
}
Serial.println("LittleFS started");


{
const char *txtFileName = "/jargon.txt";
const char *fzFileName = "/out.fz";

unsigned int srcBufLen;
unsigned char *srcBuf;


{
// load the uncompressed text file into memory
File fin = LittleFS.open(txtFileName);
if(!fin)
{
Serial.println("Unable to read input file, halting");
while(1);
}
srcBufLen = fin.size();
srcBuf = (unsigned char *)malloc(srcBufLen+1);
if( srcBuf == NULL )
{
Serial.printf("Failed to malloc() %d bytes, halting.\nHint: use a smaller file.\n", srcBufLen);
while(1);
}
size_t bytesRead = fin.readBytes( (char*)srcBuf, srcBufLen);
printf("Loaded %d bytes from file into ram\n", bytesRead);
fin.close();
}


{
if( LittleFS.exists(fzFileName))
LittleFS.remove(fzFileName); // delete artifacts from previous test

// open the fz file for writing
File fout = LittleFS.open(fzFileName, "w");
if(!fout)
{
Serial.println("Unable to create output file, halting");
while(1);
}

// deflate!
size_t outputSize = LZPacker::compress( srcBuf, srcBufLen, &fout );
Serial.printf("Compressed %d input bytes to %u raw bytes, file size is %d bytes\n", srcBufLen, outputSize, fout.position() );
free(srcBuf); // free the buffer
fout.close();
}


{
// now open the fz file for reading
File flz = LittleFS.open(fzFileName);
if(!flz)
{
Serial.println("Unable to open lz file, halting");
while(1);
}

// create GzUnpacker instance
GzUnpacker *GZUnpacker = new GzUnpacker();
// attach callback to write the uncompressed data
GZUnpacker->setStreamWriter(
[](unsigned char* buff, size_t buffsize)->bool
{
Serial.write(buff, buffsize); return true;
}
);
// inflate!
GZUnpacker->gzStreamExpander(&flz, flz.size());
}

}


}

void loop()
{

}
Binary file not shown.
Loading

0 comments on commit 62e69a6

Please sign in to comment.